检查视图中的用户角色时出现SQL Server连接错误

时间:2015-05-07 17:39:12

标签: c# asp.net sql-server asp.net-mvc razor-2

当我把它放在我的Index.cshtml中时:

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

它抛出了这个错误:

  

类型&#39; System.Web.HttpException&#39;的例外情况发生在System.Web.dll中但未在用户代码中处理

其他信息:

  

无法与SQL Server数据库连接

一切都与SQL Server有关,它创建数据库,创建用户。

我已经查看了其他问题,但没有任何答案可以提供帮助!

更新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")]

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

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

<roleManager enabled="true" />

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

MVC 5 - RoleManager