集成ASP.NET身份的最佳做法 - 它们是否存在? [英] Best practices for integrating ASP.NET Identity - do they exist?

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

问题描述

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

  public class IdentityUser:User 
{
public virtual DomainUser DomainUser {get;私人集合
}

我已经移动了 DbSet 同样派生的 DbContext 类作为我的域模型如本答案所示。我已经通过Fluent API将 IdentityUser 单向链接到 DomainUser

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

这允许我主要将授权和身份验证的问题与 DomainUser 类。这比把它们组合成一个班更好,但是它仍然感觉很丑。我仍然在我的域项目中引用了所需的ASP.NET标识程序集。我可以创建另一个项目,只保留我的IdentityUser类和对我的Domain程序集的引用,以允许导航属性,但是这开始变得复杂。



我觉得应该有一个更好,更干净,更模块化的方式来将身份连接到域,而不会导致紧密耦合。有没有人想出更好的处理方法?我希望引起参与ASP.NET身份项目的人的关注( Hao Kung 等)

解决方案

事实是,如果你要继承自IdentityUser,ASP.NET Identity相关的程序集正在来搭乘。您不能将Identity与ASP.NET分离。问题中的原始示例不要你买东西 - 在大多数情况下,如果要使用 IdentityUser ,则可能更好$ c> IdentityUser 在您的域项目。



如果您的域名项目真的需要免费的ASP.NET相关的程序集,那么你可以离开您的Web项目中的Identity相关类,并在您的域项目中创建一个单独的用户模型,以编程方式链接两个,如 here


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

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();

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.

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.

解决方案

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.

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天全站免登陆