shenku shenku - 2 months ago 23
C# Question

Network related error while trying to authenticate against a role?

I feel silly asking this but hey, its happening.

I am authenticating my MVC controller using:


I have a custom Identity like this which is populated with roles correctly:

public class AppIdentity : IIdentity
public AppIdentity(string fullName, Guid id, string[] roles)
Name = fullName;
Id = id;
Roles = roles;

public AppIdentity(FormsAuthenticationTicket ticket)
if (ticket == null || ticket.Name == null)
throw new ArgumentNullException("ticket");

string[] data = ticket.Name.Split(new[] { "||" }, StringSplitOptions.None);
Name = data[0];
Id = new Guid(data[1]);
Roles = data[2].Split(',');

public string[] Roles { get; set; }

And I authenticate in my global like this:

protected void Application_AuthenticateRequest(object sender, EventArgs e)
var cookie = Request.Cookies[FormsAuthentication.FormsCookieName];
var ticket = TryDecryptCookie(cookie);

if (ticket != null)
var identity = new AppIdentity(ticket);
var principal = new GenericPrincipal(identity, identity.Roles);

Context.User = principal;

This all seems to be good at this point, cookies are there, identity is created correctly with the roles etc.

But then when I hit the controller, it seems to try and create a database in my appdata folder, with an error of:

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)

I have removed all of the Membership provider stuff from my web.config, so ?why is it trying to do this?


A bit more:

This is from the AuthorizeAttribute:

IPrincipal user = httpContext.User;
if (!user.Identity.IsAuthenticated || this._usersSplit.Length > 0 && !Enumerable.Contains<string>((IEnumerable<string>) this._usersSplit, user.Identity.Name, (IEqualityComparer<string>) StringComparer.OrdinalIgnoreCase) || this._rolesSplit.Length > 0 && !Enumerable.Any<string>((IEnumerable<string>) this._rolesSplit, new Func<string, bool>(user.IsInRole)))
return false;
return true;

You can see it asks the IPrinciple if it is in the role.

And in the GenericPrinciple class:

for (int index = 0; index < this.m_roles.Length; ++index)
if (this.m_roles[index] != null && string.Compare(this.m_roles[index], role, StringComparison.OrdinalIgnoreCase) == 0)
return true;

So it really shouldn't be trying to connect to a DB right?


Found the answer here: It's related to web.config.