MVC 5和ASP.NET身份 - 实施混乱 [英] MVC 5 & ASP.NET Identity - Implementation Confusion

查看:111
本文介绍了MVC 5和ASP.NET身份 - 实施混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建将使用的 MVC 5 实体框架数据库第一种方法编写新的Web应用程序。我也想使用 ASP.Net身份照顾会员,认证,授权等。

我读过有关的ASP.Net身份好位在网络上,它是如何工作的,但是,我仍然在学习这个话题。

当我创造了我在Visual Studio 2013 MVC 5应用程序,看着帐户控制器我的第一反应是,我不喜欢我所看到的,即一个的DbContext 被所引用名为 ApplicationDbContext 。我不喜欢这样做的原因是因为我preFER保持我的DbContext在合适的项目我的解决方案,即内,在坚持以关注的分离逻辑。<我的模型层/ p>

此外,开箱MVC 5项目使用实体框架code首先创建一个默认的数据库和表来存储用户,角色等。

由于我必须与现有的用户表使用现有的数据库,这种方式不适合我的需求。

我还是想用最新的ASP.Net身份对我的应用程序,因为它看起来有很多好处,因此,我发现这篇文章它剥去了很多实体框架code,但仍然得到了OWIN供电认证成一个ASP.NET MVC。

<一个href=\"http://www.khalidabuhakmeh.com/asp-net-mvc-5-authentication-breakdown-part-deux\">http://www.khalidabuhakmeh.com/asp-net-mvc-5-authentication-breakdown-part-deux

使用上面的教程,这里是在 HttpPost登录作为方法我帐户控制器

  [HttpPost]
    [使用AllowAnonymous]
    公众的ActionResult登录(LoginViewModel型号,串RETURNURL)
    {
        如果(ModelState.IsValid)
        {
            //调用这验证用户登录信息我自己的自定义账户服务
            VAR用户= _AccountService.VerifyPassword(model.UserName,model.Password,FALSE);
            如果(用户)
            {
                VAR身份=新ClaimsIdentity(新[] {新的索赔(ClaimTypes.Name,model.UserName)},DefaultAuthenticationTypes.ApplicationCookie,ClaimTypes.Name,ClaimTypes.Role);                // TODO:手动添加的角色,但以后会从数据库中拉
                identity.AddClaim(新索赔(ClaimTypes.Role,客人));                AuthenticationManager.SignIn(新AuthenticationProperties
                {
                    IsPersistent = model.RememberMe
                },身份);                返回RedirectToAction(指数,MyDashboard);
            }
            其他
            {
                ModelState.AddModelError(,无效的用户名或密码。);
            }
        }        返回查看(模型);
    }

在我的previous MVC应用程序,我通常推出自己的自定义成员,当用户登录到该网站,并通过身份验证,我会存储在的任何其他用户的详细信息,如用户ID,出生日期等的UserData 的字符串在的FormsAuthenticationTicket

由于code以上不使用 FormsAuthentication ,而是使用 OWIN CookieAuthentication ,我不知道如何来存储这些额外的用户数据。

因此​​,我对我遇到的问题的几个问题。


  1. 我如何保存用户ID或任何其他附加件的用户数据(DOB等)我FormsAuthentication习惯的方式?这是通过添加一个自称的身份呢?


  2. 使用是否的上述方法ASP.Net身份/ OWIN似乎正确的考虑,我使用实体框架数据库先用现有的数据库?


  3. 我应该使用的开箱code,它在账户控制器,也就是说,的UserManager,ApplicationUser,ApplicationDbContext等使用,并钩住这与我现有的数据库工作?


我道歉,如果我的问题是混乱的,我想我只是有点不确定我应该用什么办法,同时试图使用ASP.Net身份在我的最新项目。

任何反馈会大大AP preciated。

感谢。


解决方案

1)新的武士刀的Cookie中间件支持索赔。这是什么使这比窗体身份验证cookie的更好;声称模型中的任何键/值对和那些可存储在认证的cookie。看到这个帖子了解更多详情:

<一个href=\"http://brockallen.com/2013/10/24/a-primer-on-owin-cookie-authentication-middleware-for-the-asp-net-developer/\" rel=\"nofollow\">http://brockallen.com/2013/10/24/a-primer-on-owin-cookie-authentication-middleware-for-the-asp-net-developer/

2及3)至于你的存储身份数据,如果需要与现有的表的工作,那么你可能无法使用微软的EF提供的类。相反,你会留在自己的执行IUserStore和所有其他存储器接口你的应用需求。我不能确定这是值得您已经使用来存储用户数据的变化是什么

请该OWIN /武士刀一部分是从身份存储分开。

I'm creating a new web application that will be written using MVC 5 and Entity Framework Database First Approach. I would also like to use ASP.Net Identity to look after membership, authentication, authorisation etc.

I've read a good bit about the ASP.Net Identity on the web and how it works, however, I am still learning about this topic.

When I created my MVC 5 application in Visual Studio 2013 and looked at the Account Controller my first instinct was that I didn't like what I saw, i.e., a DbContext was being referenced named 'ApplicationDbContext'. The reason I didn't like this was because I prefer to keep my DbContext in the appropriate project within my solution, i.e., in my Model layer which adheres to the separation of concerns logic.

Also, the out of the box MVC 5 project uses Entity Framework Code First to create a default database and tables to store the Users, Roles etc.

Because I must use an existing database with an existing User table, this approach does not suit my needs.

I still want to use the latest ASP.Net Identity for my application as it looks to have many benefits, therefore, I found this article which stripped back alot of the Entity Framework code but still got OWIN powered authentication into an ASP.NET MVC.

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

Using the tutorial above, here is the HttpPost Login method for my Account Controller

    [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);
    }

In my previous MVC applications I usually rolled my own custom membership and when a User logged into the site and was authenticated, I would have stored the any additional user details such as userID, DOB etc in the UserData string of the FormsAuthenticationTicket.

As the code above does not use FormsAuthentication, instead it uses OWIN CookieAuthentication, I am not sure how to store this additional user data.

Therefore, I have a few questions about the problems I am experiencing.

  1. How do I store the userID or any other additional piece of user data (DOB etc) the way I used to in FormsAuthentication? Is this done by adding a Claim to the identity?

  2. Does the method of using ASP.Net Identity/ OWIN above seem correct considering I am using Entity Framework Database First with an existing database?

  3. Should I be using the out of the box code that is used in the Account Controller, i.e., UserManager, ApplicationUser, ApplicationDbContext etc and hooking this up to work with my existing database?

I apologise if my question is confusing, I suppose I'm just a little unsure of what approach I should be using whilst attempting to use ASP.Net Identity in my latest project.

Any feedback would be greatly appreciated.

Thanks.

解决方案

1) The new Katana Cookie middleware supports claims. This is what makes this better than forms auth cookie; claims model any key/value pair and those can be stored in the authentication cookie. See this post for more details:

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

2 & 3) As far as your storage for identity data, if you need to work with an existing table then you might not be able to use Microsoft's EF provided classes. Instead you'd be left on your own to implement IUserStore and all the other store interfaces your app needs. I'm not certain it's worth changing what you're already using to store the user data.

Keep in mind that the OWIN/Katana part is separate from the identity storage.

这篇关于MVC 5和ASP.NET身份 - 实施混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆