1

I'm using various resources to try and implement an Identity system with MS Access for an AngularJS app. I created classes which implement the Identity interfaces I need, and I'm stuck at the stage of creating the Account controller (which will be the API for registeration, login, etc).

The class UserStore implements IUserStore and has the CreateAsync method:

public Task CreateAsync(TUser user)
{
    if (user == null)
    {
        throw new ArgumentNullException("user");
    }

    var result = userTable.Insert(user);

    return Task.FromResult(result);
} 

AccountController implements ApiController and has the Register method:

[AllowAnonymous]
[Route("register")]
public async Task<IHttpActionResult> Register(IdentityUser user)
{
    var result = await _userStore.CreateAsync(user);

    if (result == 0)
    {
        return InternalServerError();
    }

    return Ok();
}

userTable.Insert(user) returns an int indicating the number of rows affected in the DB table. The line var result = await _userStore.CreateAsync(user); throws an error, saying it actually returns void, and so void cannot be assigned to var (or to anything else).

I'm having a hard time understanding how to write the Register method and the CreateAsync method so that they will work together.

BTW, I thought I should give up the whole async thing and just make CreateAsync and Register return the int value as-is, but I can't do that since UserStore implements `IUserStore'.

OzW
  • 848
  • 1
  • 11
  • 24
  • 1
    There's an example in [this question](http://stackoverflow.com/questions/32927571/iuserstoretuser-createasync-how-to-indicate-failure-in-custom-implementation) which may help – stuartd Apr 12 '16 at 11:10

1 Answers1

1

The issue is that the return type cannot be passed from the CreateAsync as it is simply a Task return. It would need to be Task<int> but you cannot do that since it's implementing the IUserStore interface. Why do you need the result, I'm assuming you do not?

Try this instead:

[AllowAnonymous]
[Route("register")]
public async Task<IHttpActionResult> Register(IdentityUser user)
{
    await _userStore.CreateAsync(user);
    return Ok();
}

Additionally, consider making userTable.Insert(user) an async call if at all possible.

I would suggest not giving up on async/await. Especially for I/O bound operations on a web site like this, they really make your application usable.

If you're really concerned about whether or not the insert might be problematic, try this instead:

public async Task CreateAsync(TUser user)
{
    if (user == null)
    {
        throw new ArgumentNullException("user");
    }

    var existingUser = await this.FindByIdAsync(user.Id);
    if (existingUser != null)
    {
        await this.UpdateAsync(user);
    }
    else
    {
        userTable.Insert(user);
    }
}
David Pine
  • 23,787
  • 10
  • 79
  • 107
  • Can you please help me with this question:http://stackoverflow.com/questions/39275597/how-to-give-custom-implementation-of-updateasync-method-of-asp-net-identity – I Love Stackoverflow Sep 01 '16 at 15:30
  • @Learning if you're targeting **ASP.NET** and not **ASP.NET Core**, I am not as familiar with that... I have been working on a huge **ASP.NET Core** project therefore I'm more up-to-speed with this framework, so to speak. – David Pine Sep 01 '16 at 16:15
  • Yes I am targetting asp.net core and your this answer shows that you have good understanding on Microsoft asp.net identity.can you give me any idea or suggestion on how to update password hashed with Microsoft identity in database table?? – I Love Stackoverflow Sep 01 '16 at 16:25