How to check user role in MVC 5 identity?

一世执手 提交于 2019-12-11 13:33:52

问题


I create a EF code first database identity system with custom roles. When i register a user i put him in a role, and in the database i can see that works on the AspNetUserRoles table.

But when i try to check the user role using User.IsInRole("admin") i.e, it always return false( i tried with low, upper, all the way case). I need to put something in the webconfig? Or what im missing here? Its my first time developing with this new Identity system and i think that is a bit confuse.

In my immediate window:

User.Identity.Name
"teste06"
User.Identity.IsAuthenticated
true
User.IsInRole("admin")
A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in     System.Data.dll

Exception:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)


回答1:


It seems that the role Manager Is Activated and it is trying to create an SQL Express Database to validate roles, i 've had the same issue i solve it by adding this code to my web.config file

<system.webServer>
  <modules>
    <remove name="RoleManager" />
  </modules>
</system.webServer>



回答2:


You have to make sure you have this in your startup class

app.UseSignInCookies();

Also, I'm guessing your DbContext class is derived from IdentityDbContext<ApplicationUser>or IdentityDbContext.

If not, make sure you do.

Look this article for reference.

Similar post relating to your problem can be found here



来源:https://stackoverflow.com/questions/21996191/how-to-check-user-role-in-mvc-5-identity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!