SQL 在视图中检查用户角色时出现服务器连接错误

SQL Server connection error when checking User Roles inside a view

当我把这个放在我的 Index.cshtml:

   @if (Request.IsAuthenticated && User.IsInRole("Admin"))
                {
                    <li><a href="#">Gerenciar</a></li>
                }

它抛出这个错误:

An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code

附加信息:

It is not possible to connect with the SQL Server database

一切正常SQL服务器相关,它创建数据库,创建用户很好。

我已经查看了其他问题,但没有任何答案对您有帮助!

更新 1

我使用默认的 Identity 2.0 上下文初始化我的上下文:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    static ApplicationDbContext()
    {
        // Set the database intializer which is run once during application start
        // This seeds the database with admin user credentials and admin role
        Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public System.Data.Entity.DbSet<Gatos.Models.Gato> Gato { get; set; }

    public System.Data.Entity.DbSet<Gatos.Models.Formulario> Formulario { get; set; }
    public System.Data.Entity.DbSet<Gatos.Models.Imagem> Imagem { get; set; }
}

我的连接字符串:

<add name="DefaultConnection" connectionString="Data Source=Bruno-PC;Initial Catalog=Gatos;User Id = sa; Password = *******" providerName="System.Data.SqlClient" />

更新 2

现在我的:

[Authorize(Roles="Admin")]

在我的控制器中抛出同样的错误!这让我发疯!

您是否使用自定义会员资格进行身份验证/授权?

作为快捷方式,尝试从主 Web.Config 中删除以下行:

<roleManager enabled="true" />

此外,请查看有关此问题和解决方案的以下页面:

MVC 5 - RoleManager

尝试在连接字符串而不是星号 (*) 中设置密码

<add name="DefaultConnection" connectionString="Data Source=Bruno-PC;Initial Catalog=Gatos;User Id = sa; Password = your_db_pwd" providerName="System.Data.SqlClient" />