我正在使用Entity Framework 5 Database First方法开发MVC 5 Web应用程序.我正在使用OWIN进行用户身份验证.下面显示我的帐户控制器中的登录方法.
public ActionResult Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = _AccountService.VerifyPassword(model.UserName, model.Password, false);
if (user != null)
{
var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, model.UserName), }, DefaultAuthenticationTypes.ApplicationCookie, ClaimTypes.Name, ClaimTypes.Role);
identity.AddClaim(new Claim(ClaimTypes.Role, "guest"));
identity.AddClaim(new Claim(ClaimTypes.GivenName, "A Person"));
identity.AddClaim(new Claim(ClaimTypes.Sid, user.userID)); //OK to store userID here?
AuthenticationManager.SignIn(new AuthenticationProperties
{
IsPersistent = model.RememberMe
}, identity);
return RedirectToAction("Index", "MyDashboard");
}
else
{
ModelState.AddModelError("", "Invalid username or password."); …Run Code Online (Sandbox Code Playgroud) asp.net authentication asp.net-mvc claims-based-identity asp.net-mvc-5
我无法理解整个ASP.Net Identity框架,我首先尝试将Identity 1.0集成到现有应用程序中,但我最终得到了2个DbContext类,我的应用程序数据在MyDbContext上,而IdentityDbContext上的Identity用户数据.
我想对我的应用程序数据和身份相关数据使用单个DbContext,我在一个不同的问题上发现我可以像使用DbContext一样使用IdentityDbContext; 但是,我想我错过了一些东西.
我正在使用Identity团队的示例项目
PM> Install-Package Microsoft.AspNet.Identity.Samples -Pre
Run Code Online (Sandbox Code Playgroud)
并尝试将DbSet属性插入ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
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 virtual DbSet<Student> students { get; set; }
public virtual DbSet<Teachers> teachers { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用ApplicationDbInitializer种子方法来填充这两个表,如下所示: …