整合ASP.NET身份的最佳实践 - 他们存在吗? [英] Best practices for integrating ASP.NET Identity - do they exist?

查看:106
本文介绍了整合ASP.NET身份的最佳实践 - 他们存在吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET身份与一个新的网站,并有不似乎是如何做到这一点的去耦方式很多(任何?)的例子。我不希望我的域模型的 DomainUser 类必须从 Microsoft.AspNet.Identity.EntityFramework.User 继承,所以我创建了一个类,它看起来像这样:

I'm using ASP.NET Identity with a new website and there don't seem to be many (any?) examples of how to do this in a decoupled manner. I do not want my domain model's DomainUser class to have to inherit from Microsoft.AspNet.Identity.EntityFramework.User, so I've created a class that looks like this:

public class IdentityUser : User
{
    public virtual DomainUser DomainUser { get; private set; }
}

我搬到了 DbSet 取值要求ASP.NET身份进入相同的派生的DbContext 类作为我的域模型<一个href=\"http://stackoverflow.com/questions/19308959/set-oauth-connectionstring-property-in-net-4-5-1/19913398#19913398\">as在这个答案说明。我已经通过流畅API链接的 IdentityUser 单向到 DomainUser 像这样:

I've moved the DbSets required by ASP.NET Identity into the same derived DbContext class as my domain models as illustrated in this answer. I've linked the IdentityUser unidirectionally to the DomainUser via Fluent API like so:

modelBuilder.Entity<IdentityUser>().HasRequired(iu => iu.DomainUser).WithRequiredPrincipal();

这让我大多从 DomainUser 类中定义的行为单独授权和认证的关注。这比将它们合并为一类更好,但仍觉得难看。我还是要在我的领域工程所需的ASP.NET身份集的引用。我可以创建另一个项目,该项目举行了只有我IdentityUser类和引用我的域名装配以允许导航属性,但开始觉得费解。

This allows me to mostly separate the concerns of authorization and authentication from the behaviors defined in the DomainUser class. This is better than combining them into one class, but it still feels ugly. I still have references to the required ASP.NET Identity assemblies in my Domain project. I could create yet another project that held only my IdentityUser class and a reference to my Domain assembly to allow for the navigation property, but that starts to feel convoluted.

我觉得应该有一个更好,更清洁,更模块化的方式,没有它导致紧密耦合链接身份到域。

I feel like there should be a better, cleaner, more modular way to link Identity up to the domain without it resulting in tight coupling.

有没有人拿出处理这个更好的办法?我希望能争取那些参与ASP.NET身份项目的关注(郝骷嗯等)这里提供的方向。

Has anyone come up with a better way of handling this? I'm hoping to garner the attention of those involved in the ASP.NET Identity project (Hao Kung et al) to provide direction here.

推荐答案

事实是,如果你打算从IdentityUser继承,ASP.NET身份相关的组件凑凑热闹到来。您无法从ASP.NET独立的身份。在这个问题原来的例子不买,你在大多数情况下much-你可能会更好过,从 IdentityUser 只是继承,如果你打算使用 IdentityUser 在域项目。

The fact is that if you're going to inherit from IdentityUser, ASP.NET Identity related assemblies are coming along for the ride. You can't separate Identity from ASP.NET. The original examples in the question don't buy you much- in most cases you're probably better off just inheriting from IdentityUser if you're going to be using IdentityUser in your Domain project.

如果您的域名项目真正需要是免费的ASP.NET相关的组件,然后你可以离开了身份相关的类在Web项目,并创建一个单独的用户模型您的域项目的链路相连的两个编程,所建议的<一个href=\"http://stackoverflow.com/questions/28974687/where-should-ef-migrations-go-my-class-library-project-or-my-asp-net-project/28975106#28975106\">here.

If your domain project truly needs to be free of ASP.NET related assemblies then you can leave the Identity related classes in your web project and create a separate User model in your Domain project an link the two programmatically, as suggested here.

这篇关于整合ASP.NET身份的最佳实践 - 他们存在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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