我是ASP.NET身份的新手,并且仍在设法了解它的工作原理。不幸的是,我发现我尝试过的许多教程都是针对Identity 1.0的,而我却尝试使用Identity 2.0。

我面临的最大问题是我认为很简单,但事实并非如此。我正在尝试做的是将现有数据库与现有用户表一起使用。目前,我似乎能做的就是让Identity创建自己的表来存储用户数据。我不想使用 Entity Framework ,但是在将 Entity Framework 与Identity 2.0分离时遇到了很大的困难。

在我使用SQL Server的情况下,我想尝试通过以下链接为MySQL实现自定义提供程序:http://www.asp.net/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-providerhttps://github.com/raquelsa/AspNet.Identity.MySQL。这似乎仅在身份1下起作用。确实有一个较新的版本,但是使用的是 Entity Framework 。我也尝试过https://github.com/ILMServices/RavenDB.AspNet.Identity

在尝试了所有方法后,我陷入了以下困境:

var manager = new IdentityUserManager(new UserStore<IdentityUser>(context.Get<ApplicationDbContext>()));

我面临的问题是我需要根据RavenDB的说明删除ApplicaitonDbContext类,但是我不知道在该行中放入什么。

我得到的错误是:







第二个错误是可以预期的,但是我不确定在此行代码中使用什么。有没有人在不使用 Entity Framework 的情况下实现自定义提供程序的经验?

谢谢。

更新1:

我尝试了以下教程[http://aspnetguru.com/customize-authentication-to-your-own-set-of-tables-in-asp-net-mvc-5/],但它似乎是不完整的,在完全遵循该教程时会出现编译或运行时错误。我仍然遇到的一些问题是...

用“用户”替换“ApplicationUser”的所有实例时,IdentityConfig.cs中的以下行有问题
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<MyDbContext>()));

将ApplicationUser更改为User会产生以下错误



我也很难确定如何使用用户管理器和许多ASync方法。以下行不起作用:
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(UserManager));

在AccountController.cs中,出现以下错误:

最佳答案

在您的示例中,您似乎想将DbContext替换为其他内容,但我相信实际上,您需要将精力集中在更高一级。

该框架具有UserManager类,该类负责管理(但不存储)用户及其相关信息。当它想存储用户(或他们的相关信息)时,默认设置是使用提供的UserStore<IdentityUser>,它知道如何使用IdentityUserDbContext实例存储在数据库中。

在2.0框架中,身份存储的各个位被分解为几个接口(interface)。提供的默认实现UserStore<IdentityUser>实现了其中一些接口(interface),并使用DBContext将所有接口(interface)的数据存储在数据库中。

如果查看默认提供的基于 Entity Framework 的UserStore的定义,您会发现它实现了许多这些小接口(interface):

public class UserStore<TUser, TRole, TKey, TUserLogin, TUserRole, TUserClaim> : IUserLoginStore<TUser, TKey>,
IUserClaimStore<TUser, TKey>, IUserRoleStore<TUser, TKey>, IUserPasswordStore<TUser, TKey>,
IUserSecurityStampStore<TUser, TKey>, IQueryableUserStore<TUser, TKey>, IUserEmailStore<TUser, TKey>,
IUserPhoneNumberStore<TUser, TKey>, IUserTwoFactorStore<TUser, TKey>, IUserLockoutStore<TUser, TKey>,
IUserStore<TUser, TKey>, IDisposable
where TUser : IdentityUser<TKey, TUserLogin, TUserRole, TUserClaim>
where TRole : IdentityRole<TKey, TUserRole>
where TKey : Object, IEquatable<TKey>
where TUserLogin : new(), IdentityUserLogin<TKey>
where TUserRole : new(), IdentityUserRole<TKey>
where TUserClaim : new(), IdentityUserClaim<TKey>

由于您根本不想使用Entity Framework,因此需要提供一些关键接口(interface)的自己的实现,这些接口(interface)会将数据存储在要存储数据的位置。主界面是 IUserStore<Tuser,TKey> 。这定义了用于存储具有特定类型 key 的用户的契约(Contract)。如果您只想存储用户而没有其他信息,则可以实现此接口(interface),然后将实现传递给UserManager,您应该一切顺利。

尽管您可能希望为用户提供密码,角色,登录名等,但这不可能足够。如果是这样,那么您需要使实现IUserStore<Tuser,TKey>的类也实现IUserPasswordStore<TUser, TKey>IRoleStore<TRole, TKey>IUserClaimStore<TUser, TKey>

您可以找到所有接口(interface)的列表on MSDN here

根据要使用的位,需要实现这些接口(interface)。

您可能还需要定义自己的Entity framework already out of the box类版本的Identity*类。因此,您将需要自己的IdentityUser类,该类代表您要存储的用户,并且需要IdentityUserClaim用于用户声明。我自己尚未完成此操作,因此我不确定,但是我的感觉是您将必须执行此操作。

斯科特·艾伦(Scott Allen)在模型的各个位上都有一个good article,可能有用。

至于您对此行的问题:
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(UserManager));

有一种称为GenerateUserIdentityAsync的方法,该方法在Applicationuser上定义,该方法扩展了IdentityUser。我相信它是在模板项目中定义的,看起来像这样:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(
    UserManager<ApplicationUser> manager) {
    // Note the authenticationType must match the one
    // defined in CookieAuthenticationOptions.AuthenticationType
    var userIdentity =
        await manager.CreateIdentityAsync(this,
            DefaultAuthenticationTypes.ApplicationCookie);
    // Add custom user claims here
    return userIdentity;
    }
}

由于不再使用 Entity Framework IdentityUser,因此您需要定义类似的方法或自己的User类或以其他方式实现相同的功能(例如仅调用await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie))

关于c# - 带有自定义表的Identity 2.0,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24428520/

10-14 16:35
查看更多