MVC使用Membership配置

MVC的权限管理,环境是MVC4.5,SQL Server2008

 

修改前

Web.config文件:

<system.web>

    <authentication mode="None" >
    </authentication>

</system.web>

  <system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
    </modules>
  </system.webServer>

 

修改后

<system.web>
    <authentication mode="Forms" >
      <forms loginUrl="~/Account/Login">
      </forms>
    </authentication>

// <authentication mode="Forms" >:身份认证的方式


    <membership defaultProvider="AspNetSqlMembershipProvider">
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider"
             type="System.Web.Security.SqlMembershipProvider"
             connectionStringName="MappingConnectionStr"
             enablePasswordRetrieval="false"
             enablePasswordReset="true"
             requiresQuestionAndAnswer="false"
             applicationName="/"
             requiresUniqueEmail="false"
             passwordFormat="Hashed"
             maxInvalidPasswordAttempts="5"
             minRequiredPasswordLength="7"
             minRequiredNonalphanumericCharacters="0"
             passwordAttemptWindow="10"
             passwordStrengthRegularExpression=""/>
      </providers>
    </membership>

// type="System.Web.Security.SqlMembershipProvider":membership的数据类型,connectionStringName:连接字符串(connectionStrings节点下的name)


    <roleManager defaultProvider="DefaultRoleProvider" enabled="true" cacheRolesInCookie="true">
      <providers>
        <add name="DefaultRoleProvider"
             connectionStringName="MappingConnectionStr"
             type="System.Web.Security.SqlRoleProvider"
             applicationName="/"/>
      </providers>
    </roleManager>

//connectionStringName:数据库连接字符串,type="System.Web.Security.SqlRoleProvider":roleManager的数据类型


  </system.web>

 

 

 

controller:

[Authorize(Roles="RoleName",Users="UserName")]

public ActionResult Login()

{

}

 

 

Membership使用:

Membership类方法

 

SQL Server:

创建权限管理相关表

文件路径:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319  找到文件夹下aspnet_regsql.exe
 
  出现:ASP.NET SQL Server 安装向导
 
 
点击下一步:
 
点击为应用程序配置SQL Server
SQL验证后,选择存储数据库,点击下一步完成
 
数据库生成:
 

 

posted @ 2016-03-30 11:23  笑语载歌  阅读(767)  评论(0编辑  收藏  举报