【发布时间】:2015-01-16 00:35:45
【问题描述】:
我有一个需要非常低级别的身份验证和登录功能的 asp mvc 应用程序。用户有一个唯一的 7 位数字,他们将输入该数字以登录应用程序。一旦通过身份验证,他们就可以编辑 EF 数据库。就是这样!
目前我正在使用 Owin 的基本实现来验证和登录用户。基本上我有一个输入字段,用户在其中输入他们的号码,然后我使用 EF 查找它,如果我找到了我创建声明并登录的号码。
我对此很陌生,我看不到使用 Asp Identity 系统的好处,因为我不需要外部登录功能或注册功能。此外,我没有用户资料。我只需要一种方法来检查这些数字以识别用户是否在数据库中,然后将它们登录。所以问题是:我的唯一 Owin 实现是否有效?为什么我需要在这里使用 asp Identity 框架?
[HttpPost, AllowAnonymous, ValidateInput(true)]
public ActionResult Index(employeePSDB lookUpEmployeeID)
{
if (ModelState.IsValid)
{
using (var db = new PsEntities())
{
IEnumerable<employeePSDB> foundEmployeeInDb = db.employeePSDBs.Where(foundEmployee => lookUpEmployeeID.EmployeeNumber == foundEmployee.EmployeeNumber);
if (foundEmployeeInDb.Count() > 0)
{
var identity = new ClaimsIdentity(new[] {
new Claim(ClaimTypes.Name, foundEmployeeInDb.First().EmployeeNumber.ToString())
}, "ApplicationCookie");
var contex = Request.GetOwinContext();
var authManager = contex.Authentication;
authManager.SignIn(identity);
return Redirect(GetRedirectUrl(lookUpEmployeeID.ReturnUrl));
}
}
}
//user authN failed or ModelState is not valid
ModelState.AddModelError("", "Not in db");
employeePSDB newLookUp = new employeePSDB
{
ReturnUrl = lookUpEmployeeID.ReturnUrl
};
return View(newLookUp);
}
【问题讨论】:
标签: c# asp.net-mvc entity-framework asp.net-identity owin