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'.

1
  • 1
    There's an example in this question which may help Commented Apr 12, 2016 at 11:10

1 Answer 1

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);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can you please help me with this question:stackoverflow.com/questions/39275597/…
@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.
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??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.