如何检查MVC中5用户身份的角色? [英] How to check user role in MVC 5 identity?

查看:82
本文介绍了如何检查MVC中5用户身份的角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个EF code第一个数据库的身份与自定义角色系统。当我注册一个用户我把他放在一个角色,并在数据库中我可以看到,在AspNetUserRoles表工作。

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.

但是,当我尝试使用检查User.IsInRole(管理员),即用户的角色,它始终返回false(我试过低,上,一路的情况下)。
我需要把一些在webconfig?或者有什么即时在这里丢失?它的我第一次用这个新身份的系统开发,我认为这是一个有点混淆。

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.

在我的眼前窗口:

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

例外:

建立SQL Server的连接时发生网络相关的或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server配置为允许远程连接。 (商SQL网络接口,错误:26 - 错误定位服务器/实例指定)

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)

推荐答案

看来,角色管理器被激活
它试图创建一个SQL防爆preSS数据库
验证角色,我已经加入这个code到有同样的问题我解决我的
web.config文件

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>

这篇关于如何检查MVC中5用户身份的角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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