Exemplo n.º 1
0
    public async Task <IActionResult> AddClaimsToUser(AccountClaimsViewModel vm)
    {
        if (!ModelState.IsValid)
        {
            ModelState.AddModelError(string.Empty, "数据异常!");
            return(View());
        }
        var user = await userManager.FindByIdAsync(vm.UserId);

        if (user == null)
        {
            ModelState.AddModelError(string.Empty, "用户不存在!");
            return(View());
        }
        user.Claims.Add(new IdentityUserClaim <string>
        {
            ClaimType  = vm.UserClaim,
            ClaimValue = vm.UserClaim
        });
        var result = await userManager.UpdateAsync(user);

        if (result.Succeeded)
        {
            return(RedirectToAction(nameof(Edit), new { Id = vm.UserId }));
        }
        foreach (var error in result.Errors)
        {
            ModelState.AddModelError(string.Empty, error.Description);
        }
        return(View(vm));
    }
Exemplo n.º 2
0
    public async Task <IActionResult> AddClaimsToUser(string userId)
    {
        var user = await userManager.FindByIdAsync(userId);

        if (user == null)
        {
            return(Content("用户不存在!"));
        }

        var userClaims = await userManager.GetClaimsAsync(user);

        var claims    = Enum.GetValues(typeof(AuthClaimEnum));
        var claimList = new List <string>();

        foreach (var c in claims)
        {
            if (userClaims.Count(x => x.Value.Equals(c.ToString())) <= 0)
            {
                claimList.Add(c.ToString());
            }
        }

        var vm = new AccountClaimsViewModel
        {
            UserId = userId,
            Claims = claimList
        };

        return(View(vm));
    }