When I try to use Roles.IsUserInRole("roleX") or any related method I get a database connection issue, as can be seen here. 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. 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.
-
Check the connection strings to verify they're looking at the same location? – mason Apr 25 '17 at 15:06
-
They are, I have checked this multiple times. Thanks though! – LeRainman Apr 25 '17 at 15:09
-
You can find the solution steps in this question http://stackoverflow.com/questions/18060667/why-am-i-getting-cannot-connect-to-server-a-network-related-or-instance-speci. Mark this question as duplicate. – Naveen K N Apr 25 '17 at 15:10
-
I do not believe any of those are my issue, as I can connect to the database fine. As stated above the connection issue only happens when trying to access roles in a matter of IsUserInRole or [Authenticate]. During normal none role use the database is accessed fine, i can read, write etc no problem. – LeRainman Apr 25 '17 at 15:27
-
What kind of authentication are you using? Identity or FormsAuth? – Erik Funkenbusch Apr 25 '17 at 15:29
-
I am using Identity – LeRainman Apr 25 '17 at 15:46
-
Does your connection string use integrated security, or does it specify a user/password? – Apr 25 '17 at 17:14
-
it specifies a user and password. – LeRainman Apr 25 '17 at 18:48
-
Are you using Entity Framework? – nurdyguy Apr 25 '17 at 20:44
1 Answers
Your problem is that Roles.IsUserInRole("roleX")
is not part of Identity framework. This is part of MembershipProvider and you don't want to use it. Reason for getting this error - MembershipProvider tries to be helpful and attempts connecting to a database, a database you never told it about.
If you need to check if current user is in a role use User.IsInRole("RoleX");
. Where User
is part of a Controller or a View. Or you can also do it via 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).
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()

- 34,305
- 22
- 140
- 234
-
1Good catch, I didn't notice the use of the Roles class, versus User. – Erik Funkenbusch Apr 26 '17 at 18:26