标识2.0自定义表 [英] Identity 2.0 with custom tables

查看:375
本文介绍了标识2.0自定义表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来ASP.NET的身份和我仍然试图让我的头围绕如何工作的。不幸的是,我发现了很多我试过是身份1.0,而我试图与Identity 2.0上班教程。

我现在面临的最大问题是什么我以为会很简单,但事实并非如此。我想要做的是使用现有的数据库与现有的用户表。目前,所有我似乎可以做的就是身份来创建自己的表来存储用户数据。我不希望使用实体框架,但我已经从Identity 2.0分离实体框架很大的困难。

虽然我的情况我使用SQL服务器,我想我会尝试在这个环节实现MySQL的自定义提供程序:<一href=\"http://www.asp.net/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider\" rel=\"nofollow\">http://www.asp.net/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider和 https://github.com/raquelsa/AspNet.Identity.MySQL 。这似乎在身份仅1工作。目前确实出现了一个新的,但是使用实体框架。我也试着 https://github.com/ILMServices/RavenDB.AspNet.Identity

使用的一切我已经尝试了,我得到坚持与以下行:

  VAR经理=新IdentityUserManager(新UserStore&LT; IdentityUser&GT;(context.Get&LT; ApplicationDbContext&GT;()));

我现在面临的问题是,我需要根据对RavenDB说明删除ApplicaitonDbContext类,但是我不知道要放什么东西在这一行吧。

我得到的错误是:


  

非泛型类型'AspNet.Identity.MySQL.UserStore不能与类型参数使用



  

类型或命名空间名称'ApplicationDbContext'找不到(是否缺少using指令或程序集引用?)


第二个错误是可以预期的,但我不知道在这行code的使用什么来代替。有没有人有实现自定义提供不使用实体框架?

任何经验

感谢。

更新1:

我已经尝试下面的教程[的http://aspnetguru.com/customize-authentication-to-your-own-set-of-tables-in-asp-net-mvc-5/ ],但它究竟会出现以下教程时,是不完整的,具有编译或运行时错误。有几个问题我还是有...

在使用用户取代ApplicationUser的所有实例都在IdentityConfig.cs以下行问题

  VAR经理=新ApplicationUserManager(新UserStore&LT; ApplicationUser&GT;(context.Get&LT; MyDbContext&GT;()));

更改ApplicationUser给用户产生以下错误


  

类型WebApplicationTest2.Models.User'不能用作在通用类型或方法Micr​​osoft.AspNet.Identity.EntityFramework.UserStore'类型参数TUSER'。有一个从WebApplicationTest2.Models.User'到'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'没有隐式引用转换。


我也有困难的工作如何使用用户管理器和许多异步方法。下面的行不工作:

  AuthenticationManager.SignIn(新AuthenticationProperties(){IsPersistent = isPersistent},等待user.GenerateUserIdentityAsync(的UserManager));

在与以下错误AccountController.cs:


  

对于最佳重载方法匹配'Microsoft.Owin.Security.IAuthenticationManager.SignIn(Microsoft.Owin.Security.AuthenticationProperties, PARAMS System.Security.Claims.ClaimsIdentity [])'有一些无效的参数。


  
  

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



解决方案

在你的例子,你似乎想用别的东西来代替的DbContext 但我beleive,实际上你需要关注你的任务之一水平。

骨架具有类的UserManager 里面有管理,但不存储,用户及其相关信息的责任。当它要存储用户(或他们的相关信息)默认是使用所提供的 UserStore&LT; IdentityUser&GT; 它知道如何存储 IdentityUser 使用数据库实例的的DbContext

在2.0框架的身份存储的各个位被分为几个接口。所提供的默认实现, UserStore&LT; IdentityUser&GT; 实现了几个使用这些接口并存储所有这些数据在数据库中的的DbContext

如果你看一下基于UserStore你可以看到默认提供的实体框架的defnition,它实现了许多这些小接口:

 公共类UserStore&LT; TUSER,TRole,TKEY的,TUserLogin,TUserRole,TUserClaim&GT; :IUserLoginStore&LT; TUSER,TKEY的&gt;中
IUserClaimStore&LT; TUSER,TKEY的&gt;中IUserRoleStore&LT; TUSER,TKEY的&gt;中IUserPasswordStore&LT; TUSER,TKEY的&gt;中
IUserSecurityStampStore&LT; TUSER,TKEY的&gt;中IQueryableUserStore&LT; TUSER,TKEY的&gt;中IUserEmailStore&LT; TUSER,TKEY的&gt;中
IUserPhoneNumberStore&LT; TUSER,TKEY的&gt;中IUserTwoFactorStore&LT; TUSER,TKEY的&gt;中IUserLockoutStore&LT; TUSER,TKEY的&gt;中
IUserStore&LT; TUSER,TKEY的&gt;中了IDisposable
其中,TUSER:IdentityUser&LT; TKEY的,TUserLogin,TUserRole,TUserClaim&GT;
其中,TRole:IdentityRole&LT; TKEY的,TUserRole&GT;
其中,TKEY的:对象,IEquatable&LT; TKEY的&GT;
其中,TUserLogin:新的(),IdentityUserLogin&LT; TKEY的&GT;
其中,TUserRole:新的(),IdentityUserRole&LT; TKEY的&GT;
其中,TUserClaim:新的(),IdentityUserClaim&LT; TKEY的&GT;

因为你不希望使用Entity Framework的所有,你需要提供一些将这些数据存储在您要存储数据的地方,这些关键接口的自己的实现。主界面是<一个href=\"http://msdn.microsoft.com/en-us/library/dn613278(v=vs.108).aspx\"><$c$c>IUserStore<Tuser,TKey>.此定义用于存储具有特定类型的密钥的用户的合同。如果你只想要存储的用户并没有其他信息,您可以实现此接口,然后通过你的执行情况向的UserManager ,你应该是好去。

它不太可能,这将是足够的,虽然同样可能你会想密码,角色,登录等,为您的用户。如果是这样,那么你需要让你的类,它实现 IUserStore&LT; TUSER,TKEY的&GT; 也实施 IUserPasswordStore&LT; TUSER,TKEY的&GT; IRoleStore&LT; TRole,TKEY的&GT; IUserClaimStore&LT; TUSER,TKEY的方式&gt;

您可以href=\"http://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity(v=vs.108).aspx\">上找到所有的接口

根据您要使用你需要实现这些接口的位。

您可能还需要定义自己所存在的<一个在身份* 类版本href=\"http://msdn.microsoft.com/en-us/library/microsoft.aspnet.identity.entityframework(v=vs.108).aspx\">Entity框架已经开箱的。所以,你会需要它重新prersents要存储的用户, IdentityUserClaim 为你自己的 IdentityUser 类用户索赔。我还没有完成这件事自己,所以我不能完全肯定,但我的感觉是,你将不得不这样做。

斯科特 - 阿伦有好文章上可能是有用的。该模型的各个位

对于这一行的问题:

  AuthenticationManager.SignIn(新AuthenticationProperties(){IsPersistent = isPersistent},等待user.GenerateUserIdentityAsync(的UserManager));

有一个名为 GenerateUserIdentityAsync 方法,它是在 Applicationuser 延伸定义的 IdentityUser 。它是在模板项目,我相信定义,看起来是这样的:

 公共类ApplicationUser:IdentityUser
{
公共异步任务&LT; ClaimsIdentity&GT; GenerateUserIdentityAsync(
    的UserManager&LT; ApplicationUser&GT;经理) {
    //注意authenticationType必须的一场比赛
    //在CookieAuthenticationOptions.AuthenticationType定义
    VAR的UserIdentity =
        等待manager.CreateIdentityAsync(这一点,
            DefaultAuthenticationTypes.ApplicationCookie);
    //添加自定义的用户在这里声明
    返回的UserIdentity;
    }
}

当你不使用实体框架 IdentityUser 任何更多的,那么你要么需要定义一个类似的方法或您自己的用户类或implmenet相同的功能其他方式(如只调用等待manager.CreateIdentityAsync(这一点,DefaultAuthenticationTypes.ApplicationCookie)代替)

I'm new to ASP.NET identity and am still trying to get my head around how it all works. Unfortunately I've found many of the tutorials I've tried are for Identity 1.0, whereas I'm attempting to work with Identity 2.0.

The biggest problem I am facing is something I thought would be simple, but has turned out not to be. What I'm trying to do is use an existing database with an existing user table. Currently all I can seem to do is get Identity to create it's own tables to store user data. I don't want to use Entity Framework, but I've had great difficulty separating Entity Framework from Identity 2.0.

While my situation I'm using SQL Server, I thought I'd try and implement the custom provider for MySQL at this link: http://www.asp.net/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider and https://github.com/raquelsa/AspNet.Identity.MySQL. This appears to work under Identity 1 only. There does appear to be a newer one, however that uses Entity Framework. I've also tried https://github.com/ILMServices/RavenDB.AspNet.Identity.

With everything I've tried, I get stuck with the following line:

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

The problem I'm facing is that I need to remove ApplicaitonDbContext class according to the instructions for RavenDB, however I don't know what to put in this line instead.

The errors I get are:

The non-generic type 'AspNet.Identity.MySQL.UserStore' cannot be used with type arguments

and

The type or namespace name 'ApplicationDbContext' could not be found (are you missing a using directive or an assembly reference?)

The second error is to be expected, however I'm not sure what to use in this line of code instead. Has anyone had any experience with implementing a custom provider without using Entity Framework?

Thanks.

Update 1:

I've attempted the following tutorial [ http://aspnetguru.com/customize-authentication-to-your-own-set-of-tables-in-asp-net-mvc-5/ ] but it appears to be incomplete, with compile or runtime errors when following the tutorial exactly. A few problems I still have are...

When replacing all instances of "ApplicationUser" with "User" have a problem with the following line in IdentityConfig.cs

var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<MyDbContext>()));

Changing ApplicationUser to User produces the following error

The type 'WebApplicationTest2.Models.User' cannot be used as type parameter 'TUser' in the generic type or method 'Microsoft.AspNet.Identity.EntityFramework.UserStore'. There is no implicit reference conversion from 'WebApplicationTest2.Models.User' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'.

I'm also having difficulty working out how to use the user manager and many of the ASync methods. The following lines don't work:

AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(UserManager));

In AccountController.cs with the following errors:

The best overloaded method match for 'Microsoft.Owin.Security.IAuthenticationManager.SignIn(Microsoft.Owin.Security.AuthenticationProperties, params System.Security.Claims.ClaimsIdentity[])' has some invalid arguments.

'WebApplicationTest2.Models.User' does not contain a definition for 'GenerateUserIdentityAsync' and no extension method 'GenerateUserIdentityAsync' accepting a first argument of type 'WebApplicationTest2.Models.User' could be found (are you missing a using directive or an assembly reference?)

解决方案

In you examples you seem to want to replace the DbContext with something else but I beleive that actually you need to focus you efforts one level higher.

The framework has a class UserManager which has the responsibility for managing, but not storing, the users and their related information. When it wants to store the users (or their related information) the default is to use the provided UserStore<IdentityUser> which knows how to store IdentityUser instances in a database using a DbContext.

In the 2.0 framework the various bits of the identity storing were broken into several interfaces. The default implementation provided, UserStore<IdentityUser> implements several of these interfaces and stores the data for them all in the database using a DBContext.

If you look at the defnition of the default provided entity framework based UserStore you can see that it implements many of these small interfaces:

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>

As you don't want to use Entity Framework at all, you need to provide your own implementations of some of these key interfaces which will store that data in the places you want the data to be stored. The main interface is IUserStore<Tuser,TKey>. this defines the contract for storing users which have a key of a particular type. If you only want to store users and no other information the you can implement this interface and then pass your implementation to the UserManager and you should be good to go.

Its unlikely that this will be enough though as likely you will want passwords, roles, logins etc for your users. If so then you need to make your class which implements IUserStore<Tuser,TKey> also implement IUserPasswordStore<TUser, TKey>, IRoleStore<TRole, TKey> and IUserClaimStore<TUser, TKey>.

You can find a list of all the interfaces on MSDN here

Depending on the bits you want to use you need to implement those interfaces.

You will probably also need to define your own versions of the Identity* classes which exist for the Entity framework already out of the box. So you will need your own IdentityUser class which reprersents the users you want to store, and IdentityUserClaim for the users claims. I haven't done this myself so I'm not exactly sure, but my feeling is that you will have to do this.

Scott Allen has a good article on the various bits of the model which might be useful.

As for your issues with this line:

AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(UserManager));

There is a method called GenerateUserIdentityAsync which is defined on Applicationuser which extends IdentityUser. It is defined in the template project I believe and looks something like this:

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

As you are not using the entity framework IdentityUser any more then you either need to define a similar method or your own User class or implmenet the same functionality some other way (like just calling await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie) instead)

这篇关于标识2.0自定义表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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