C# 带有自定义表的Identity 2.0

C# 带有自定义表的Identity 2.0,c#,asp.net,asp.net-mvc-5,asp.net-identity-2,C#,Asp.net,Asp.net Mvc 5,Asp.net Identity 2,我对ASP.NET identity还不熟悉,我仍在努力了解它是如何工作的。不幸的是,我发现我尝试过的许多教程都是针对Identity 1.0的,而我正在尝试使用Identity 2.0 我面临的最大问题是一些我认为很简单的事情,但事实证明并非如此。我要做的是将现有数据库与现有用户表一起使用。目前,我所能做的似乎就是获取Identity来创建自己的表来存储用户数据。我不想使用实体框架,但我很难将实体框架从Identity 2.0中分离出来 在我使用SQL Server的情况下,我想我应该尝试在以

我对ASP.NET identity还不熟悉,我仍在努力了解它是如何工作的。不幸的是,我发现我尝试过的许多教程都是针对Identity 1.0的,而我正在尝试使用Identity 2.0

我面临的最大问题是一些我认为很简单的事情,但事实证明并非如此。我要做的是将现有数据库与现有用户表一起使用。目前,我所能做的似乎就是获取Identity来创建自己的表来存储用户数据。我不想使用实体框架,但我很难将实体框架从Identity 2.0中分离出来

在我使用SQL Server的情况下,我想我应该尝试在以下链接实现MySQL的自定义提供程序:和。这似乎仅适用于标识1。不过,似乎有一个更新的版本使用实体框架。我也试过了

无论我做了什么,我都会被下面这句话困住:

var manager = new IdentityUserManager(new UserStore<IdentityUser>(context.Get<ApplicationDbContext>()));
在AccountController.cs中出现以下错误:

“Microsoft.Owin.Security.IAAuthenticationManager.SignIn(Microsoft.Owin.Security.AuthenticationProperties,params System.Security.Claims.ClaimsIdentity[])的最佳重载方法匹配项具有一些无效参数

“WebApplicationTest2.Models.User”不包含“GenerateUserIdentityAsync”的定义,并且找不到接受类型为“WebApplicationTest2.Models.User”的第一个参数的扩展方法“GenerateUserIdentityAsync”(是否缺少using指令或程序集引用?)


我对自己的身份很陌生。 您的用户实体是否实现了IUser接口?e、 g

public partial class User : IUser<Guid> //Whatever your key is
{
    public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
    {
        return Task.FromResult(GenerateUserIdentity(manager));
    }

    public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = manager.CreateIdentity<User, Guid>(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

在您的示例中,您似乎想用其他内容替换
DbContext
,但我认为,实际上,您需要将精力集中在更高的层次上

该框架有一个类
UserManager
,负责管理但不存储用户及其相关信息。当它想要存储用户(或其相关信息)时,默认情况下使用提供的
UserStore
,它知道如何使用
DbContext
在数据库中存储
IdentityUser
实例

在2.0框架中,身份存储的各个部分被分解成几个接口。提供的默认实现,
UserStore
实现了其中几个接口,并使用
DBContext
将所有接口的数据存储在数据库中

如果您查看默认提供的基于实体框架的UserStore的定义,您会发现它实现了以下许多小接口:

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>
有一个名为
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;
    }
}
公共类应用程序用户:IdentityUser
{    
公共异步任务GenerateUserIdentityAsync(
用户管理器(管理器){
//注意,authenticationType必须与
//在CookieAuthenticationOptions.AuthenticationType中定义
var userIdentity=
等待manager.CreateIdentityAsync(此,
DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
}

由于您不再使用实体框架
IdentityUser
,因此您需要定义一个类似的方法或自己的
User
类,或者以其他方式实现相同的功能(例如只需调用
wait manager.createidentitysync(这是DefaultAuthenticationTypes.applicationcokie)

我认为最近在
ApplicationUser
中对此进行了更新:

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
{
    // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
    var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
    // Add custom user claims here
    return userIdentity;
}

这看起来是我拥有的最好的机会,看起来很有希望,而且赏金即将到期,所以我把它给了你。谢谢,乔希,你能把它弄好吗?我现在也在考虑做同样的事情。帮了我不少忙。这里有一篇很好的文章:
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>
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(UserManager));
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;
    }
}
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager, string authenticationType)
{
    // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
    var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
    // Add custom user claims here
    return userIdentity;
}
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager,
    OAuthDefaults.AuthenticationType);
ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync(UserManager,
    CookieAuthenticationDefaults.AuthenticationType);