当前位置: 动力学知识库 > 问答 > 编程问答 >

How to use ASP.NET Role and Membership in Winform and C#

问题描述:

I tried to convert this VB.NET sample here

http://www.theproblemsolver.nl/usingthemembershipproviderinwinforms.htm

without any success, can anyone know an example ?

Thanks

网友答案:

I just started getting into C# and I'm trying to develop a Windows Form application as well. I am using C# 2010 Express (Target framework = .NET Framework 4). This is a sample of my results from converting this.

Program.cs

using System;
using System.Security.Principal;
using System.Web;
using System.Web.Security;


class UserRoles
{

    static void Main(string[] args)
    {
    MembershipCreateStatus result;

    Membership.CreateUser("testuser", "Pass!", "[email protected]", "Hood", "Pine Hills", true, out result);
    Console.WriteLine(result.ToString());

    Roles.CreateRole("Developer");
    Roles.AddUserToRole("testuser", "Developer");

   if (Roles.IsUserInRole("testuser","developer")) 
       Console.WriteLine("Is a developer");
   else
       Console.WriteLine("Doesn't write code.");

    if (Membership.ValidateUser("testuser", "Pass!")) 
       Console.WriteLine("User Validated.");
   else
       Console.WriteLine("User Invalid");

   Console.ReadKey();
    }
}

App.config

<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>

  <system.web>
    <roleManager enabled="true" />
    <membership>
      <providers>
           <remove name="AspNetSqlMembershipProvider"/>
        <add name="AspNetSqlMembershipProvider"
             type="System.Web.Security.SqlMembershipProvider, System.Web,
           Version=2.0.0.0, Culture=neutral,
           PublicKeyToken=b03f5f7f11d50a3a"
             connectionStringName="LocalSqlServer"
             enablePasswordRetrieval="false"
             enablePasswordReset="false"
             requiresQuestionAndAnswer="false"
             applicationName="/"
             requiresUniqueEmail="false"
             passwordFormat="Hashed"
             maxInvalidPasswordAttempts="3"
             minRequiredPasswordLength="4"
             minRequiredNonalphanumericCharacters="1"
             passwordAttemptWindow="10"
             passwordStrengthRegularExpression="" />
      </providers>
    </membership>
  </system.web>  
</configuration>

These are some other links I had tried to work through but they were tougher for me to understand at this stage in the game.

Client Application Services

Custom Fluent Nhibernate Membership and Role Provider

分享给朋友:
您可能感兴趣的文章:
随机阅读: