asp.net-mvc-5 - MVC 5 和 ASP.NET Identity - 实现困惑

标签 asp.net-mvc-5 asp.net-identity owin

我正在创建一个新的 Web 应用程序,该应用程序将使用 MVC 5 Entity Framework 数据库优先方法编写。我还想使用 ASP.Net Identity 来管理成员资格、身份验证、授权等。

我已经在网络上阅读了大量有关 ASP.Net Identity 及其工作原理的内容,但是,我仍在学习这个主题。

当我在 Visual Studio 2013 中创建 MVC 5 应用程序并查看帐户 Controller 时,我的第一直觉是我不喜欢我所看到的,即 DbContext strong> 被名为“ApplicationDbContext”的引用。我不喜欢这个的原因是因为我更喜欢将 DbContext 保留在解决方案中的适当项目中,即在遵循关注点分离逻辑的模型层中。

此外,开箱即用的 MVC 5 项目使用 Entity Framework Code First 创建默认数据库和表来存储用户、角色等。

因为我必须使用现有的数据库和现有的 User 表,所以这种方法不适合我的需求。

我仍然想在我的应用程序中使用最新的 ASP.Net Identity,因为它看起来有很多好处,因此,我发现这篇文章剥离了大量 Entity Framework 代码,但仍然在 ASP 中获得了 OWIN 支持的身份验证。 NET MVC。

http://www.khalidabuhakmeh.com/asp-net-mvc-5-authentication-breakdown-part-deux

使用上面的教程,这是我的帐户 Controller HttpPost Login方法

    [HttpPost]
    [AllowAnonymous]
    public ActionResult Login(LoginViewModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            //Calling my own custom Account Service which validates users login details
            var user = _AccountService.VerifyPassword(model.UserName, model.Password, false);
            if (user)
            {
                var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, model.UserName), }, DefaultAuthenticationTypes.ApplicationCookie, ClaimTypes.Name, ClaimTypes.Role);

                //ToDo: Manually adding Role, but will pull from db later
                identity.AddClaim(new Claim(ClaimTypes.Role, "guest"));

                AuthenticationManager.SignIn(new AuthenticationProperties
                {
                    IsPersistent = model.RememberMe
                }, identity);

                return RedirectToAction("Index", "MyDashboard");
            }
            else
            {
                ModelState.AddModelError("", "Invalid username or password.");
            }
        }

        return View(model);
    }

在我以前的 MVC 应用程序中,我通常会推出自己的自定义成员(member)资格,当用户登录网站并通过身份验证时,我会将任何其他用户详细信息(例如 userID、DOB 等)存储在 UserDataFormsAuthenticationTicket 的 Strong> 字符串。

由于上面的代码没有使用FormsAuthentication,而是使用OWIN CookieAuthentication,所以我不确定如何存储这些额外的用户数据。

因此,我对我遇到的问题有一些疑问。

  1. 如何按照 FormsAuthentication 中的方式存储用户 ID 或任何其他附加用户数据(DOB 等)?这是通过向身份添加声明来完成的吗?

  2. 考虑到我将 Entity Framework Database First 与现有数据库一起使用,上述使用 ASP.Net Identity/OWIN 的方法似乎正确吗?

  3. 我是否应该使用帐户 Controller 中使用的现成代码(即 UserManager、ApplicationUser、ApplicationDbContext 等)并将其连接到我现有的数据库?

如果我的问题令人困惑,我深表歉意,我想我只是有点不确定在我的最新项目中尝试使用 ASP.Net Identity 时应该使用什么方法。

如有任何反馈,我们将不胜感激。

谢谢。

最佳答案

1) 新的 Katana Cookie 中间件支持声明。这就是它比表单验证 cookie 更好的原因;声明对任何键/值对建模,并且这些可以存储在身份验证 cookie 中。请参阅这篇文章了解更多详细信息:

http://brockallen.com/2013/10/24/a-primer-on-owin-cookie-authentication-middleware-for-the-asp-net-developer/

2 和 3) 就身份数据的存储而言,如果您需要使用现有表,那么您可能无法使用 Microsoft 的 EF 提供的类。相反,您需要自己实现 IUserStore 以及您的应用程序所需的所有其他商店接口(interface)。我不确定是否值得更改您已用于存储用户数据的内容。

请记住,OWIN/Katana 部分与身份存储是分开的。

关于asp.net-mvc-5 - MVC 5 和 ASP.NET Identity - 实现困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21385582/

相关文章:

asp.net-mvc - 禁用并指定 Html.DropDownList 的默认选择

asp.net-mvc - 如何更改 Microsoft.AspNet.Identity.EntityFramework.IdentityUser 中的 id 类型

asp.net-mvc - MVC5 中的自定义验证错误消息

c# - 格式化十进制 JsonResult ASP MVC 5

c# - 如何删除默认的 ASP.NET Core Identity 端点?

c# - MySQL 和 EF6 的 ASP.NET 身份迁移问题

asp.net-core - HttpContext.User.Claims 和 IHttpContextAccessor 登录成功后均返回空值

asp.net-mvc - MVC6 自托管 wwwroot 内容返回 404,IIS Express 不会

c# - 生成由操作过滤器控制的 PDF?

asp.net-mvc - 卸载 OWIN 和 Identity 包