Search code examples
c#asp.netasynchronousasp.net-identity

asp.net identity - SetPasswordHashAsync


I'm trying to configure identity to work with custom provider. So far all good, I've created UserStore:

public class UserStore : IUserStore<User>, IUserPasswordStore<User>

(with all methods implemented) and UserManager:

public UserManager(IUserStore<User> userStore)
        : base(userStore)
    { }

and User is my custom entity to hold users.

The problem is that in UserStore I have the following:

public async Task SetPasswordHashAsync(User user, string passwordHash)

From what I have understood, this is called before user is created, so all I should do here is:

public Task SetPasswordHashAsync(User user, string passwordHash)
{
    user.PasswordHash = passwordHash;
}

... but this has to return a Task and I don't see anything asynchronous to be done here.

  1. Why return type is Task for SetPasswordHashAsync and not just void?
  2. How should I implement SetPasswordHashAsync?

I know I can do something like

public async Task SetPasswordHashAsync(User user, string passwordHash)
{
    user.PasswordHash = passwordHash;
    await Task.FromResult(0);
}

... but isn't this synchronous? It is weird to see all other methods decorated with async Task and use await, and this one to return Task.FromResult()

Anyone?


Solution

  • Tasks do not have to be asynchronous. You can implement it as follows:

    public Task SetPasswordHashAsync(User user, string passwordHash)
    {
        user.PasswordHash = passwordHash;
        return Task.FromResult(0);
    }