[英]Authorize(Roles=“Admin”) does not work
I'm writing ASP.NET application with Identity as authorization engine. 我正在使用Identity作为授权引擎编写ASP.NET应用程序。 I wrote my custom user and role stores and they seem to work correctly.
我编写了自定义用户和角色存储,它们似乎正常工作。 However, for some unknown reason, attempt to use
Authorize
with role on controller's action fails and redirects to Home/Index
. 但是,由于某些未知原因,尝试在控制器操作上使用
Authorize
with role失败并重定向到Home/Index
。 The action looks like following: 该操作如下所示:
[Authorize(Roles = "Admin")]
public ActionResult Manage()
{
return View();
}
The redirection is being done silently, so I cannot hook debugger anywhere (especially that action filtering is being done before running action). 重定向是以静默方式完成的,因此我无法将调试器挂钩到任何地方(尤其是在运行操作之前正在执行动作过滤)。
I guess this may not be enough to diagnose the problem, so just tell me in comments, what additional information do you need and I'll edit the question. 我想这可能不足以诊断问题,所以请在评论中告诉我,您需要哪些其他信息,我将编辑问题。
Why doesn't it work? 为什么不起作用?
Edit : Configuration in Startup.cs
编辑 :Startup.cs中的配置
public void Configuration(IAppBuilder app)
{
DataProtectionProvider = app.GetDataProtectionProvider();
app.CreatePerOwinContext<ApplicationUserManager>(() => DependencyResolver.Current.GetService<ApplicationUserManager>());
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "ApplicationCookie",
LoginPath = new PathString("/Account/Login")
});
}
Edit: Another action:
编辑:另一个动作:
[AllowAnonymous]
public async Task<ActionResult> Index()
{
// This returns FALSE
if (User.IsInRole("Admin"))
System.Diagnostics.Debug.WriteLine("Ok");
var userManager = UnityConfig.Container.Resolve<ApplicationUserManager>();
// This returns TRUE
if (await userManager.IsInRoleAsync(User.Identity.GetUserId<int>(), "Admin"))
System.Diagnostics.Debug.WriteLine("Ok");
return View();
}
A couple of things to check: 要检查的几件事:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.