集成 ASP.NET Identity 的最佳实践——它们存在吗?

Best practices for integrating ASP.NET Identity - do they exist?(集成 ASP.NET Identity 的最佳实践——它们存在吗?)
本文介绍了集成 ASP.NET Identity 的最佳实践——它们存在吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个新网站上使用 ASP.NET Identity,但似乎没有很多(任何?)示例说明如何以分离的方式执行此操作.我不希望我的域模型的 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; }
}

我已将 ASP.NET Identity 所需的 DbSet 移动到与我的域模型相同的派生 DbContext 类中如本答案所示.我已经通过 Fluent 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 类和对我的 Domain 程序集的引用以允许导航属性,但这开始让人感到复杂.

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 Identity 项目的人的注意(Hao Kung 等)在这里提供指导.

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 Identity 相关的程序集就会出现.您不能将身份与 ASP.NET 分开.问题中的原始示例对您没有太大帮助-在大多数情况下,如果您要使用 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 相关的程序集,那么您可以将 Identity 相关的类留在您的 Web 项目中,并在您的域项目中创建一个单独的 User 模型两个以编程方式,如建议 这里.

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 Identity 的最佳实践——它们存在吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)