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使用:
SQL Server:
创建权限管理相关表
文件路径: