質問

I have a .NET Web API project that users the individual accounts. I can register users fine using the standard template AccountController. However, I now want to set up roles and add users to roles depending on the type of user.

There are no roles automatically set up in the DB. How do I set up the roles and how do I add users to the roles?

The only information I can find on this is based on the old ASP.NET Membership, so it fails on the fact that the stored procedures are not set up for it.

Have scoured forums and tutorials on MSDN and can't seem to find an example for Web API.

役に立ちましたか?

解決

You can add roles using the RoleManager...

using (var context = new ApplicationDbContext())
{
    var roleStore = new RoleStore<IdentityRole>(context);
    var roleManager = new RoleManager<IdentityRole>(roleStore);

    await roleManager.CreateAsync(new IdentityRole { Name = "Administrator" });

    var userStore = new UserStore<ApplicationUser>(context);
    var userManager = new UserManager<ApplicationUser>(userStore);

    var user = new ApplicationUser { UserName = "admin" };
    await userManager.CreateAsync(user);
    await userManager.AddToRoleAsync(user.Id, "Administrator");
}

You're right that documentation is a bit light right now. But I find that once you've worked with the RoleManager and the UserManager a bit, the API's are pretty discoverable (but perhaps not always intuitive and sometimes you have to run queries directly against the store or even the db context).

他のヒント

It took me awhile to figure out but I finally got it. Anthony please excuse me but going to repost a lot of your code so that dumb developers like me can understand.

In the latest WebAPI2 (Visual Studio 2013 Update 2) the registration method will look like so:

// POST api/Account/Register
[AllowAnonymous]
[Route("Register")]
public async Task<IHttpActionResult> Register(RegisterBindingModel model)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };

    IdentityResult result = await UserManager.CreateAsync(user, model.Password);


    if (!result.Succeeded)
    {
        return GetErrorResult(result);
    }

    return Ok();
}

What you want to do is replace it with this:

// POST api/Account/Register
[AllowAnonymous]
[Route("Register")]
public async Task<IHttpActionResult> Register(RegisterBindingModel model)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }


    IdentityResult result;
    using (var context = new ApplicationDbContext())
    {
        var roleStore = new RoleStore<IdentityRole>(context);
        var roleManager = new RoleManager<IdentityRole>(roleStore);

        await roleManager.CreateAsync(new IdentityRole() { Name = "Admin" });

        var userStore = new UserStore<ApplicationUser>(context);
        var userManager = new UserManager<ApplicationUser>(userStore);

        var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };

        result = await UserManager.CreateAsync(user, model.Password);
        await userManager.AddToRoleAsync(user.Id, "Admin");

    }

    if (!result.Succeeded)
    {
        return GetErrorResult(result);
    }

    return Ok();
}

Now when you post it should correctly work, but you may run into a further problem. After I did this my response complained about the DB.

The model backing the <Database> context has changed since the database was created

To fix this error I had to go into the Package Manager Console and enable Migrations.

Enable-Migrations –EnableAutomaticMigrations

Then:

Add Migration

Finally:

Update-Database

A good post on enabling migrations here: http://msdn.microsoft.com/en-us/data/jj554735.aspx

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top