问题
I have defined an authorization attribute like following:
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
filterContext.Result = new RedirectResult("~/Login");
return;
}
var requiredRoles = Roles.Split(Convert.ToChar(",")).ToList();
var roles = ((ClaimsIdentity)filterContext.HttpContext.User.Identity).Claims
.Where(c => c.Type == ClaimTypes.Role)
.Select(c => c.Value);
if (!roles.Any(r => requiredRoles.Contains(r)))
{
filterContext.Result = new RedirectResult("~/Subscriptions/Subscribe");
return;
}
}
For some reason this code is throwing the following exception:
Additional information: 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 already tested on different controllers like registration whether the web config file is configured properly towards my DB, and it looks fine. I'm not sure what could be the issue here?
来源:https://stackoverflow.com/questions/46027078/net-mvc-authorization-attribute-throwing-sql-network-related-exception