【问题标题】:Using Owin without Idenity在没有身份的情况下使用 Owin
【发布时间】: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


    【解决方案1】:

    我绝不是专家,也许有人可以对此进行更多扩展,但您的实现是基于有效 cookie 的身份验证。 Asp 身份框架或系统可以帮助您构建和维护模型。使用您自己的实体框架模型很好。请参阅以下链接以进行进一步研究...

    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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 2018-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多