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

守給你的承諾、 提交于 2019-12-12 04:46:29

问题


I tried to convert this VB.NET sample here http://www.theproblemsolver.nl/usingthemembershipproviderinwinforms.htm

without any success, can anyone know an example ?

Thanks


回答1:


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!", "test@test.com", "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



来源:https://stackoverflow.com/questions/787648/how-to-use-asp-net-role-and-membership-in-winform-and-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!