问题描述
我是新来ASP.NET的身份和我仍然试图让我的头围绕如何工作的。不幸的是,我发现了很多我试过是身份1.0,而我试图与Identity 2.0上班教程。
我现在面临的最大问题是什么我以为会很简单,但事实并非如此。我想要做的是使用现有的数据库与现有的用户表。目前,所有我似乎可以做的就是身份来创建自己的表来存储用户数据。我不希望使用实体框架,但我已经从Identity 2.0分离实体框架很大的困难。
虽然我的情况我使用SQL服务器,我想我会尝试在这个环节实现MySQL的自定义提供程序:http://www.asp.net/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider和。这似乎在身份仅1工作。目前确实出现了一个新的,但是使用实体框架。我也试着 https://github.com/ILMServices/RavenDB.AspNet.Identity。
使用的一切我已经尝试了,我得到坚持与以下行:
VAR经理=新IdentityUserManager(新UserStore< IdentityUser>(context.Get< ApplicationDbContext>()));
我现在面临的问题是,我需要根据对RavenDB说明删除ApplicaitonDbContext类,但是我不知道要放什么东西在这一行吧。
我得到的错误是:
and
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
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:
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自定义表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!