我有一个asp mvc应用程序,需要非常低级别的身份验证和登录功能 . 用户具有唯一的7位数字,他们将在其中输入以登录应用程序 . 经过身份验证后,他们可以编辑EF数据库 . 而已!
目前我正在使用Owin的基本实现来验证和登录用户 . 基本上我有一个输入字段,用户输入他们的号码,然后我使用EF查找,如果我找到我创建索赔的号码并签名 .
我是新手,我没有看到使用Asp Identity系统的好处,因为我不需要外部登录功能或注册功能 . 另外,我没有用户 Profiles . 我只需要一种方法来检查这些数字,以确定用户是否在DB中,然后对其进行签名 . 所以问题是:我的实现只有Owin有效吗?为什么我需要asp身份框架呢?
[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);
}
1 回答
我绝不是专家,也许有人可以对此进行更多扩展,但您的实现是一个有效的基于cookie的身份验证 . Asp身份框架或系统可以帮助您构建和维护模型 . 使用您自己的Entity Framework模型很好 . 请参阅以下链接以进行进一步研究......
http://www.khalidabuhakmeh.com/asp-net-mvc-5-authentication-breakdown http://www.khalidabuhakmeh.com/asp-net-mvc-5-authentication-breakdown-part-deux
并澄清任何困惑:http://brockallen.com/2012/06/04/membership-is-not-the-same-as-forms-authentication/