[英]ASP.NET MVC custom role provider not working
I have setup a custom role provider implemented using the code below except it seems it's never being used, and the default provider is being used instead. 我已经设置了一个使用以下代码实现的自定义角色提供程序,只是似乎从未使用过它,而是使用了默认提供程序。 When decorating the HomeController with the
[Authorize(Roles = "Administrator")]
attribute the CustomRoleProvider
constructor is being called (I only included the constructor in to see whether the breakpoint would be hit) but none of the methods are being called. 当使用
[Authorize(Roles = "Administrator")]
属性装饰HomeController时,将调用CustomRoleProvider
构造函数(我只包括该构造函数以查看是否会遇到断点),但是没有一个方法被调用。 And then I am left with a HTTP Error 401.0 - Unauthorized
page. 然后,我看到
HTTP Error 401.0 - Unauthorized
页面。
Aside from adding the necessary bits to the web.config I haven't done anything else to get Windows authentication to work. 除了将必要的位添加到web.config之外,我还没有执行其他任何操作来使Windows身份验证正常工作。 I assume it's working though because if I don't include
<allow users="*"></allow>
(obviously without the inclusion of the Authorize
attribute) I get a 401.2.: Unauthorized: Logon failed due to server configuration
error, so I assume I'm being authenticated. 我认为这是可行的,因为如果我不包含
<allow users="*"></allow>
(显然不包含Authorize
属性), 401.2.: Unauthorized: Logon failed due to server configuration
得到401.2.: Unauthorized: Logon failed due to server configuration
错误, 401.2.: Unauthorized: Logon failed due to server configuration
,所以我假设我正在被认证。
I've cleared my browser cookies as per this SO post but that had no effect. 我已经按照这篇SO帖子清除了我的浏览器cookie,但这没有任何效果。
CustomerRoleProvider CustomerRoleProvider
public class CustomRoleProvider : RoleProvider
{
public CustomRoleProvider()
{
}
public override bool IsUserInRole(string username, string roleName)
{
bool isUserInRole = false;
// omitted for brevity
return isUserInRole;
}
public override string[] GetRolesForUser(string username)
{
string[] roles = null;
// omitted for brevity
return roles;
}
// omitted for brevity
}
web.config web.config
<authentication mode="Windows">
</authentication>
<authorization>
<allow users="*"></allow>
<deny users="?"/>
</authorization>
<roleManager defaultProvider="CustomRoleProvider" enabled="true">
<providers>
<clear />
<add name="CustomRoleProvider" type="ProjectName.UI.Mvc.Code.Providers.CustomRoleProvider" />
</providers>
</roleManager>
Home Controller 家庭控制器
[Authorize(Roles = "Administrator")]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
This was caused from not having IIS Express set to use Windows Authentication. 这是由于未将IIS Express设置为使用Windows身份验证引起的。 To fix I selected the project in Solution Explorer, opened the Properties window set
Windows Authentication = Enabled
and Anonymous Authentication = Disabled
. 要修复,我在解决方案资源管理器中选择了项目,打开了属性窗口,设置
Windows Authentication = Enabled
和Anonymous Authentication = Disabled
。 Custom role provider now works. 自定义角色提供程序现在可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.