ASP.NET身份检查用户角色不工作 [英] ASP.NET Identity check user roles is not working

查看:141
本文介绍了ASP.NET身份检查用户角色不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC 5应用程序。我使用的用户和角色管理标准的ASP.NET身份提供商。重要的是,我使用的是从自己的仓库项目IdentityUser,但这似乎确定。我可以注册,登录,用户编辑和管理自己的角色。

I have an ASP.NET MVC 5 application. I'm using the standard ASP.NET Identity provider for user and role management. It is important that I'm using the IdentityUser from an own repository project, but this seems ok. I can register, login, edit users, and manage their roles.

我添加用户到角色的这些行:

I add user to Role with these lines:

UserManager.AddToRole(userdetail.Id, r);
db.Entry(userdetail).State = EntityState.Modified;
db.SaveChanges();

这似乎是在数据库级别工作。

This seems working in DB level.

不过,我无法使用基于角色的身份验证,实际上是simples

But, I can't use Role based authentications, actually the simples

HttpContext.User.IsInRole("Administrator")

不工作了。

[Authorize(Roles="Administrator")]

不工作了。

我可以用此方法只检查用户是否为管理员:

I can check only with this method, whether user is an administrator:

UserManager.IsInRole(userID, "Administrator").

为什么?

在每个教程中我发现了什么,一切工作正常。不同的项目库可能是什么原因呢?或ASP.NET身份坏了这么多?

In every tutorial what I found, everything works fine. The different project repository could be the reason? Or ASP.NET Identity is broken so much?

请咨询,

推荐答案

在这种情况下你需要注销并重新登录的用户。

In That Case You Need To Logout And Login The User Again.

由于该角色数据也存储在cookie中,
所以,你必须发出饼干再次去解决问题。

Because The Roles Data Is Also Stored In cookies, So You Must Issue The Cookie Again To work It.

这篇关于ASP.NET身份检查用户角色不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆