简体   繁体   English

ASP.NET MVC 5 OWIN Auhentication

[英]ASP.NET MVC 5 OWIN Auhentication

I have just started to know the MVC 5 and I am trying to use its built in owin authentication. 我刚刚开始了解MVC 5,我正在尝试使用其内置的owin身份验证。

I need to implement a forms authentication with IIS, but the OWIN Authentication is complicated than i waited. 我需要使用IIS实现表单身份验证,但OWIN身份验证比我等待的复杂。

I have az Entity Framework Model with own User, Role and RoleUser tables and want to authenticate user by these tables. 我有自己的用户,角色和RoleUser表的az实体框架模型,并希望通过这些表验证用户。

I tried to figured it out, how the owin works on a sample mvc 5 application. 我试图弄明白,owin如何在样本mvc 5应用程序上工作。 It has an ApplicationUser class: 它有一个ApplicationUser类:

public class ApplicationUser : IdentityUser
{
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
}

My main problem is the IdentityUser. 我的主要问题是IdentityUser。 It is an own ASP.NET User class with implementation of IUser interface and connection of DbContext. 它是一个自己的ASP.NET User类,具有IUser接口和DbContext连接的实现。 I have an own User POCO entity from the EF model and i do not want to mix it with an ASP.NET IUser interface. 我有一个来自EF模型的自己的用户POCO实体,我不想将它与ASP.NET IUser接口混合使用。 I do know why, but the Id of IUser interface is string type, that is also not apply to me. 我知道为什么,但IUser接口的Id是字符串类型,这也不适用于我。

The owin async user sign in is the following: owin异步用户登录如下:

private async Task SignInUserAsync(User user, bool isPersistent)
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    ClaimsIdentity identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}

It recommends to create a ClaimsIdentity type of identity to sign in user and the user property must implements IUser interface. 它建议创建一个ClaimsIdentity类型的标识来登录用户,并且user属性必须实现IUser接口。

Big question: Is the owen suggested to use in my environment? 大问题:欧文建议在我的环境中使用吗? I want to use my User Class with owin authentication, but i don't know how? 我想使用我的用户类与owin身份验证,但我不知道如何?

Thanks your help in advance! 提前感谢您的帮助!

You can ditch the entire ASP.NET Identity model and use your own components for password verification and roles/claim storage. 您可以放弃整个ASP.NET身份模型,并使用自己的组件进行密码验证和角色/声明存储。

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

Membership/Identity and Forms Auth are two different features of id and access management. 成员身份/身份和表单身份验证是身份和访问管理的两个不同功能。 Microsoft documentation does not do a good job explaining the difference between the two. Microsoft文档没有很好地解释两者之间的差异。 This is a short and sweet post that explains it. 这是一个简短而又甜蜜的帖子,可以解释它。 The article was written before OWIN, but the same principle applies. 该文章是在OWIN之前编写的,但同样的原则适用。

http://brockallen.com/2012/06/04/membership-is-not-the-same-as-forms-authentication/ http://brockallen.com/2012/06/04/membership-is-not-the-same-as-forms-authentication/

The best way I can figure is to have your POCO class derive from IdentityUser. 我能想到的最好方法是让你的POCO类派生自IdentityUser。 Doing so you can add your POCO properties as part of your new class. 这样,您可以将POCO属性添加为新类的一部分。 After this, you can instantiate a new UserManager such as: 在此之后,您可以实例化一个新的UserManager,例如:

UserManager um = new UserManager<YourPocoDerivedFromIdentityUser>(new UserStore<YourPocoDerivedFromIdentityUSer>(new ApplicationDbContext())

using OWIN will allow you to easily integrate 3rd-party authentication, such as FB, Google. 使用OWIN将允许您轻松集成第三方身份验证,例如FB,Google。 The OWIN team did a really good job integrating it with MVC. OWIN团队在与MVC的整合方面做得非常好。

If, however, your current authentication system works well I don't see why you'd wish to upgrade? 但是,如果您当前的身份验证系统运行良好,我不明白您为什么要升级?

PS - http://www.apress.com/files/extra/ASP_NET_Identity_Chapters.pdf PS - http://www.apress.com/files/extra/ASP_NET_Identity_Chapters.pdf

You can find 3 chapters on the new Identity System here. 您可以在此处找到有关新身份系统的3个章节。 They are a very good read and I highly recommend them for getting you started. 它们是一本非常好的读物,我强烈推荐它们让你开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM