I migrated my MVC application to ASP.Net Identity 2.0 from SimpleMembership and remove all reference of latter from application.
But whenever I use the authorize attribute in my controller, I come across the SQLExpress database file auto-creation error: . (BTW, I'm using Ms SQL 2012, and all other functionality is working well with EF)
[Authorize(Roles = "Admin")]
public ActionResult UserList()
{
var users = db.AspNetUsers.ToList();
return View(users);
}
I have tried all the following and still clueless on where am getting it wrong :
- Ensure no reference of SimpleMembership anyway in my app and web.config. I even removed the WebMatrix references.
- Added filters.Add(new AuthorizeAttribute()) in my startup.
Added following in start up
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 not found in my context. So meaning lazy loading is enabled by default.
Added following in my login code :
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie); ClaimsIdentity identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ExternalCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
- Yes - the role exists in correct case.