我已經設置了一個新的asp.net web窗體應用程序。我想使用我自己的實體框架數據模型,使用提供的owin認證創建一個登錄和註冊功能。實體類型ApplicationUser不是當前上下文的模型的一部分
我已將連接字符串從默認和IdentityModels.cs中更改,但使用登錄時我收到上述錯誤。
代碼與默認值不變,當我將ApplicationDBContext更改回默認連接時,它按預期工作。
我能夠從我的數據庫訪問數據,因爲我已經使用linq查詢來檢查輸入的電子郵件和密碼。
該錯誤是扔在登錄用戶的控制範圍內以下行:
var result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout: false);
有什麼我需要做的,使owin認證工作比提供的DefaultConnection以外的數據庫?
代碼段:
生成註冊:
protected void CreateUser_Click(object sender, EventArgs e)
{
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
var user = new ApplicationUser() { UserName = Email.Text, Email = Email.Text };
IdentityResult result = manager.Create(user, Password.Text);
if (result.Succeeded)
{
signInManager.SignIn(user, isPersistent: false, rememberBrowser: false);
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
ErrorMessage.Text = result.Errors.FirstOrDefault();
}
}
生成的登錄:
protected void LogIn(object sender, EventArgs e)
{
if (IsValid)
{
// Validate the user password
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();
var result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
break;
case SignInStatus.LockedOut:
Response.Redirect("/Account/Lockout");
break;
case SignInStatus.RequiresVerification:
Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}",
Request.QueryString["ReturnUrl"],
RememberMe.Checked),
true);
break;
case SignInStatus.Failure:
default:
FailureText.Text = "Invalid login attempt";
ErrorMessage.Visible = true;
break;
}
Linq查詢查找用戶:
var myUser = db.Dat_Account
.FirstOrDefault(u => u.UserName == Email.Text
&& u.Password == Password.Text);
生成ApplicationUser
public class ApplicationUser : IdentityUser
{
public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
{
return Task.FromResult(GenerateUserIdentity(manager));
}
}
我的用戶表有更多的領域不僅僅是用戶名,電子郵件和密碼。這是否有可能導致問題?
語境:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("MyDBEntities", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
你可能想在這裏http://stackoverflow.com/questions/24852194/mvc-5-usermanager-the-entity-type-applicationuser-is-not-part-of-檢查這個答案模型 – vbouk
謝謝你的迴應。我之前看過這篇文章,但不確定這個代碼是如何工作的,因爲用戶將用戶名數據傳遞給一個空的構造函數。這個用戶代碼與Visual Studio爲我生成的代碼完全不同,我無法找到解決問題的方法。我會在這篇文章中添加一些代碼。 – Ben
你可以顯示你的上下文的代碼嗎? –