.NET MVC Authorization Attribute throwing SQL network related exception

痞子三分冷 提交于 2019-12-11 07:07:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!