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);
}
私は次のすべてを試しましたが、どこが間違っているのかまだわかりません:
- とにかく、アプリとweb.configでSimpleMembershipの参照がないことを確認してください。WebMatrix 参照も削除しました。
- 私の起動時にfilters.Add(new AuthorizeAttribute())を追加 しました。
起動時に以下を追加
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);
- はい - 役割は正しいケースで存在します。