Reputation: 961
I would like to use some parts of the Identity system of Asp.NET to access a custom database, but not all of the parts. The database portion wasn't too difficult, but I am trying to find out how to write code to put to work some of the Identity methods into my custom implementation. For example one of the differing items is to not use the UserManager password hash, but instead use some other 3rd party password hash function. Some of the things I'd like to take advantage of is the cookie authentication, password validator fields, etc. So, first thing I tried to do is look at what the standard CreateAsync method does, and it takes me to a blank line looking something like this:
[AsyncStateMachine(typeof(UserManager<>.<CreateAsync>d__73))]
public virtual Task<IdentityResult> CreateAsync(TUser user, string password);
How can I make custom create-async and password-verification functions? Oh, also by 'custom database' I mean, I do not want to use the tables that come with Identity, no Roles, Claims, just my own Users table is all.
Upvotes: 2
Views: 1566
Reputation: 1149
I recently did this but there's too much code to post it all here as an answer unfortunately.
For CreateAsync you need to make a custom userstore class that implements IUserStore<your custom user>, IUserPasswordStore<your custom user>
then you need to implement the CreateAsync method:
async Task<IdentityResult> IUserStore<your custom user>.CreateAsync(your custom user user, CancellationToken cancellationToken)
{
IdentityResult result;
var tsk = Task.Factory.StartNew(() => result = someDataAccessClass.SomeSaveUserToDatabaseFUnction(user));
return await tsk;
}
You can implement the data class and save user function however you want at that point.
The password verification is a little more complex depending on what you do.
You'll have to create a custom SignInManager
that inherits from the the regular SignInManager. Then just override the PasswordSignInAsync
method with whatever you're doing security-wise. Also note that you'll need to implement some of the other functions on the UserManager like find user async and stuff like that.
Also note that for the Identity Pipeline to fill in the missing pieces you need to do services.AddIdentity().AddUserStore().AddSignInManger
(and all the other classes you customize) in your startup.cs configureservices() method and then throw app.UseIDentity()
into your configure method too.
Upvotes: 1