When i put this in my Index.cshtml:
当我把它放在我的Index.cshtml中时:
@if (Request.IsAuthenticated && User.IsInRole("Admin"))
{
<li><a href="#">Gerenciar</a></li>
}
It throws this error:
它抛出此错误:
An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code
System.Web.dll中出现“System.Web.HttpException”类型的异常,但未在用户代码中处理
Additional information:
It is not possible to connect with the SQL Server database
无法与SQL Server数据库连接
Everything is working SQL Server related, it creates the database, create users fine.
一切都与SQL Server有关,它创建数据库,创建用户很好。
I already looked at other questions and there is no answer that helps!
我已经看过其他问题了,没有答案有帮助!
UPDATE 1
I initialize my context using the default Identity 2.0 context:
我使用默认的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; }
}
My connection string:
我的连接字符串:
<add name="DefaultConnection" connectionString="Data Source=Bruno-PC;Initial Catalog=Gatos;User Id = sa; Password = *******" providerName="System.Data.SqlClient" />
Update 2
Now my:
[Authorize(Roles="Admin")]
in my controllers are throwing the same error! This is driving me insane!
在我的控制器中抛出相同的错误!这让我疯了!
2 个解决方案
#1
Try setting your password in a connection string instead of asterisk (*)
尝试在连接字符串中设置密码而不是星号(*)
<add name="DefaultConnection" connectionString="Data Source=Bruno-PC;Initial Catalog=Gatos;User Id = sa; Password = your_db_pwd" providerName="System.Data.SqlClient" />
#2
Are you using a custom Membership for authentication / Authorization?
您是否使用自定义成员身份进行身份验证/授权?
As a Shortcut, try removing the following line from your main Web.Config:
作为快捷方式,请尝试从主Web中删除以下行.Config:
<roleManager enabled="true" />
Also, take a look at the following page regarding this issue and the solution:
另外,请查看以下有关此问题和解决方案的页面:
MVC 5 - RoleManager
#1
Try setting your password in a connection string instead of asterisk (*)
尝试在连接字符串中设置密码而不是星号(*)
<add name="DefaultConnection" connectionString="Data Source=Bruno-PC;Initial Catalog=Gatos;User Id = sa; Password = your_db_pwd" providerName="System.Data.SqlClient" />
#2
Are you using a custom Membership for authentication / Authorization?
您是否使用自定义成员身份进行身份验证/授权?
As a Shortcut, try removing the following line from your main Web.Config:
作为快捷方式,请尝试从主Web中删除以下行.Config:
<roleManager enabled="true" />
Also, take a look at the following page regarding this issue and the solution:
另外,请查看以下有关此问题和解决方案的页面:
MVC 5 - RoleManager