简体   繁体   English

尝试对角色进行身份验证或访问时,ASP.NET MVC SQL Server是否不可访问?

[英]ASP.NET MVC SQL Server not accesable when trying to authenticate or access roles?

When I try to use Roles.IsUserInRole("roleX") or any related method I get a database connection issue, as can be seen here . 当我尝试使用Roles.IsUserInRole(“劳力士”)或任何相关的方法,我得到一个数据库连接的问题,可以看出这里 I can access roles in the database and read/write any time before this, so I am quite sure I do not have any connection issues. 在此之前的任何时间,我都可以访问数据库中的角色并进行读/写操作,因此,我很确定我没有任何连接问题。 All users role ID's match fine. 所有用户角色ID的匹配都很好。 The error also happens when trying to access a controller method that is authenticated to a certain role? 尝试访问已通过身份验证的控制器方法时,也会发生该错误? Once any line relating to role checking is hit the app instantly locks up and errors out after about a minute. 一旦找到与角色检查有关的任何行,该应用程序将立即锁定,并在大约一分钟后显示错误。 The only time I ever get this error is when trying to access roles, not sure if somehow identity is not connecting to the DB? 我唯一一次遇到此错误是在尝试访问角色时,不确定是否以某种方式未将身份连接到数据库? I am at a total loss on this issue, the only thing I can think of is that something with Identity is not set up right. 我在这个问题上完全不知所措,我唯一想到的就是带有身份标识的东西设置不正确。 Any ideas are greatly appreciated. 任何想法都将不胜感激。

Your problem is that Roles.IsUserInRole("roleX") is not part of Identity framework. 您的问题是Roles.IsUserInRole("roleX")不是Identity框架的一部分。 This is part of MembershipProvider and you don't want to use it. 这是MembershipProvider的一部分,您不想使用它。 Reason for getting this error - MembershipProvider tries to be helpful and attempts connecting to a database, a database you never told it about. 出现此错误的原因-MembershipProvider会尝试提供帮助,并尝试连接到您从未告知过的数据库。

If you need to check if current user is in a role use User.IsInRole("RoleX"); 如果需要检查当前用户是否在角色中,请使用User.IsInRole("RoleX"); . Where User is part of a Controller or a View. User是控制器或视图的一部分。 Or you can also do it via HttpContext.Current.User.IsInRole("RoleX"); 或者,您也可以通过HttpContext.Current.User.IsInRole("RoleX"); This checks the auth cookie for information about roles (all the roles for logged in user are persisted in auth cookie). 这将检查auth cookie以获取有关角色的信息(所有登录用户的角色都保留在auth cookie中)。

If you would like to dip into database to check for roles for an arbitrary user (not the currently logged in one) - you need to use ApplicationUserManager.IsInRoleAsync() 如果您想进入数据库以检查任意用户(而不是当前登录用户)的角色,则需要使用ApplicationUserManager.IsInRoleAsync()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM