我将我的MVC应用程序从SimpleMembership迁移到ASP.Net Identity 2.0,并从应用程序中删除后者的所有引用。
但是每当我在控制器中使用authorize属性时,我都会遇到 SQLExpress数据库文件自动创建错误:。 (顺便说一下,我正在使用Ms SQL 2012,所有其他功能都适用于EF)
[Authorize(Roles = "Admin")]
public ActionResult UserList()
{
var users = db.AspNetUsers.ToList();
return View(users);
}
我已经尝试了以下所有内容,但仍然对于错误的地方一无所知:
在启动时添加了以下内容
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
// Enable the application to use a cookie to store information for the signed in user
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
// Use a cookie to temporarily store information about a user logging in with a third party login provider
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
Configuration.ProxyCreationEnabled 。因此,默认情况下启用延迟加载。
在我的登录代码中添加了以下内容:
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
ClaimsIdentity identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ExternalCookie);
AuthenticationManager.SignIn(new AuthenticationProperties()
{
IsPersistent = isPersistent
}, identity);