[英]How to add two different login url in form based authentication in C# MVC4?
我有一个管理员登录页面,另一个有一般用户登录页面。 我已经为一般用户部分创建了一个自定义成员资格提供程序,现在我想在web.config文件中进行表单身份验证。 怎么做 ?
当我使用自定义成员资格提供程序时,我还将配置自定义角色提供程序,然后将以下行添加到我的web.config文件中。 您可以查看它是否支持您的方案。
第1步:
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<membership defaultProvider="YourCustomMembershipProviderName">
<providers>
<clear/>
<add name="YourCustomMembershipProviderName" type="Logger.SampleApp.Security.Infrustructure.CustomeMembershipProvider" connectionStringName="YourConnectionStringName" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" applicationName="Logger.SampleApp.Client.Web"/>
</providers>
</membership>
<roleManager enabled="true" defaultProvider="YourRoleProvider"
<providers>
<clear/>
<add name="YourRoleProvider" type="Logger.SampleApp.Security.Infrustructure.CustomRoleProvider" />"
</providers>
</roleManager>
第2步:
将[Authorize]
属性添加到HomeController的Index方法。
第三步:
在<appSettings>
部分下:
<add key= "enableSimpleMembership" value= "false"/>
<add key= "autoFormsAuthentication" value= "false"/>
第四步:
对AccountController
InitializeSimpleMembership
注释,并根据要求覆盖登录操作。
我们无法在webconfig文件中设置用于登录的两个登录URL。如果创建自己的自定义成员资格提供程序,则必须将其设置为defaultprovider,以使[Authorize]属性对其适用。 但就我而言,有两个提供商。 两者都是自定义提供程序,并且不允许更改默认提供程序。 一个提供程序用于管理员登录(默认提供程序),另一个提供程序用于用户登录(自定义提供程序)。 在Web配置表单中启用了身份验证
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
因此,当我使用[Authorize]属性时,它带我进入了管理员登录页面,这是预期的。 但是我需要一个属性,它将带我进入用户登录页面。 因此,我创建了一个[AuthorizeUser]属性,该属性现在将用户带到用户登录部分。
public class AuthorizeUserAttribute : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
var username = filterContext.HttpContext.User.Identity.Name;
if (username != "")
{
base.HandleUnauthorizedRequest(filterContext);
}
else
{
filterContext.Result = new RedirectToRouteResult(new
RouteValueDictionary(new { controller = "Login", action = "Index" }));
}
}
}
此属性将我的用户带到〜/ login的用户登录页面
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.