我覺得這很愚蠢,但是嘿,它的發生。嘗試通過角色進行身份驗證時出現網絡相關錯誤?
我驗證使用我的MVC控制器:
Authorize[Roles="Admin"]
我有一個自定義的身份像這樣被填充的角色正確:
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; }
而且我在全球認證是這樣的:
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;
}
這一切似乎都很好,餅乾在那裏,身份是我已經刪除了所有的
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)
:與角色等
但是當我打的控制器,它似乎試圖在我的應用程序數據文件夾中創建一個數據庫,並帶有錯誤正確創建會員提供的東西從我的web.config,所以?爲什麼它試圖做到這一點?
謝謝。
多一點:
這是從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;
else
return true;
你可以看到它要求IPrinciple如果是在中的作用。
而在GenericPrinciple類:
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;
}
所以它真的不應該試圖連接到一個數據庫嗎?
請添加web.config代碼 –
有這麼多,你想看哪個部分? – shenku
我認爲你需要刪除默認提供商 –