How to create a role using ASP.NET pre-generated database

后端 未结 1 1358
不知归路
不知归路 2021-01-26 05:40

I am using ASP.NET MVC 5. I am creating a registration page to register an \"expert\", which I want to be a separate role from a user. I see that in the pre-generated AspNetRole

相关标签:
1条回答
  • 2021-01-26 06:07

    Try this.

    private ApplicationUserManager _userManager;
    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? 
            HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }
    

    Register User like this.

    var user = new ApplicationUser {
        UserName = "John Doe",
        Email = "john.doe@foo.com"
    };
    
    // params: username and password
    var result = await UserManager.CreateAsync(user, "name_birthday_cheeky");
    if (result.Succeeded)
    {
        UserManager.AddToRole(user.Id, "expert");
    
        //code omitted for brevity
    }
    
    0 讨论(0)
提交回复
热议问题