我只是想與Owin身份驗證混在一起,想了解更多關於MVC的知識,因此決定編寫一個小測試應用程序,該應用程序可以擁有登錄表單並根據存儲在數據庫中的憑證對用戶進行身份驗證。在MVC中使用Owin進行身份驗證
帶有驗證的登錄表單現在已經寫入,但爲了上帝的緣故...我只是無法弄清楚一旦用戶輸入正確的憑證後如何創建一個有效的會話。 我想我需要設置cookie或類似的FormsAuthentication?
所以我有一個包含以下
public void Configuration(IAppBuilder app)
{
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=316888
}
public void ConfigureAuth(IAppBuilder app)
{
// Enable the application to use a cookie to store information for the signed in user
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
AuthenticationMode = AuthenticationMode.Passive,
LoginPath = new PathString("/Account/Logon"),
CookieSecure = CookieSecureOption.SameAsRequest
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
}
然後我有一個觀點含有鏈接到User.cs型號和UserController.cs控制器形式本身包含以下
型號Owin啓動文件:
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember on this computer")]
public bool RememberMe { get; set; }
/// <summary>
/// Checks if user with given password exists in the database
/// </summary>
/// <param name="_username">User name</param>
/// <param name="_password">User password</param>
/// <returns>True if user exist and password is correct</returns>
public bool IsValid(string _username, string _password)
{
using (CMSEntities dbConn = new CMSEntities())
{
_password = Encryption.GenerateSHA512String(_password);
TblUser User = dbConn.TblUsers.Where(u => u.txtUsername == _username && u.txtPassword == _password).FirstOrDefault();
if (User == null)
{
dbConn.Dispose();
return false;
} else
{
dbConn.Dispose();
return true;
}
}
}
控制器:
[HttpPost]
public ActionResult Login(Models.User user)
{
if (ModelState.IsValid)
{
if (user.IsValid(user.UserName, user.Password))
{
//Login User and redirect
}
else
{
ModelState.AddModelError("", "Login Data is Incorrect!");
}
}
return View(user);
}
對於上帝的緣故,我無法弄清楚用什麼代碼記錄用戶並創建一個cookie。 儘管經歷了很多不同的在線教程使用Owin去,我沒有得到它:(
任何建議,請
懷疑這對我有效,因爲它不是真正與Owin有關。 –
中間件用於歐文應用程序...該文章是關於使用中間件和cookie進行認證。 –