Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Add User to Role

$
0
0

Hi,

I try to master roles in asp.net web api.

I did manage to add a user and role to the standard membership database.

Now I want to put that user in the role. . .

 //Post api/Account/AddToRole   
 [Route("AddToRole")]
 public string AddToRole(UserRoleModel model)
 {
      try
      {
         var result = UserManager.AddToRole(model.UserId, model.RoleName);
         return "Ok";
      }
      catch(Exception e)
      {
         return e.Message;
      }
 }

with a UserRolModel:

 public class UserRoleModel  
    {
        [Required]
        [DataType(DataType.Text)]
        [Display (Name ="UserId")]
        public string UserId { get; set; }

        [Required ]
        [DataType(DataType.Text)]
        [Display(Name ="Role Name")]
        public string RoleName { get; set; }
    }

If I run this with postman I get the correct UserId and RoleName parameters for the UserManager.AddToRole function, but the result is a "UserId not found"

message.

If I put a breakpoint and check the UserManager I see the UserManager. users count =0!

I don't have any Idea what I am doing wrong.

Edit: there are 2 users in my UserManager . you first have to retrieve them before the count shows correct.


Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>