Claim ClaimsIdentity ClaimsPrincipal 筛选器 过滤器

.net core 3.1 AllowAnonymous特性无效

1、Startup.cs
*
*
2、PassportController.cs  LoginAsync()   Logout()

[HttpGet]
public QueryUserResponse GetPageList([FromQuery]QueryUserRequest request)
{
    var claim = (ClaimsIdentity)HttpContext.User.Identity;
    var userCode = Convert.ToInt32(claim.Claims.Where(x => x.Type.Contains("user_code")).FirstOrDefault().Value);
    return new QueryUserResponse { data = data };
}

[HttpPost("LoginAsync")]
public async Task<IActionResult> LoginAsync()
{
    var claims = new List<Claim>
    {
        new Claim(ClaimTypes.Name, "Wangdachui"),
        new Claim(ClaimTypes.NameIdentifier,"1"),
        new Claim(ClaimTypes.Role,"1")
    };

    //var claimIdentity = new ClaimsIdentity(claims, "Client1"); //ok
    //var claimIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);//ok
    var claimIdentity = new ClaimsIdentity("Cookie");//ok

    claimIdentity.AddClaim(new Claim("UserName", "Wangdachui"));
    claimIdentity.AddClaim(new Claim("Password", "123456"));
    claimIdentity.AddClaim(new Claim("RememberMe", "on"));
    claimIdentity.AddClaim(new Claim(ClaimTypes.Name, "刘德华"));
    claimIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "1"));
    claimIdentity.AddClaim(new Claim(ClaimTypes.Sid, "1"));
    claimIdentity.AddClaim(new Claim(ClaimTypes.Role, "1"));

    var claimsPrincipal = new ClaimsPrincipal(claimIdentity);
    var authProperties = new AuthenticationProperties
    {
        IsPersistent = true,//持久Cookie
        ExpiresUtc = DateTime.UtcNow.AddMinutes(30),//设置cookie过期时间
        AllowRefresh = false,
    };

    await HttpContext.SignInAsync("Client1", claimsPrincipal, authProperties);

    return RedirectToAction(nameof(HomeController.Index), "Home");
}

[HttpPost("Logout")]
public async Task<IActionResult> Logout()
{
    await HttpContext.SignOutAsync("Client1");
    return RedirectToAction(nameof(PassportController.Login), "Passport");
}

protected int UserCode
{
    get
    {
        var claim = (ClaimsIdentity)HttpContext.User.Identity;
        return Convert.ToInt32(claim.Claims.Where(x => x.Type.Contains("user_code")).FirstOrDefault().Value);
    }
}

protected string UserName
{
    get
    {
        var claim = (ClaimsIdentity)HttpContext.User.Identity;
        return claim.Claims.Where(x => x.Type.Contains("user_name")).FirstOrDefault().Value;
    }
}

*
3、PermissionAttribute.cs 过滤器

public class PermissionAttribute : ActionFilterAttribute
{
    public PermissionAttribute(IActionRepository actionRepo)
    {
        _actionRepo = actionRepo;
    }

    public override void OnActionExecuting(ActionExecutingContext context)
    {
        TokenUserInfoVo user = null;
        user = VerificationToken(token);
        var claimIdentity = new ClaimsIdentity("Cookie");
        claimIdentity.AddClaim(new Claim("UserName", user.user_name));
        claimIdentity.AddClaim(new Claim(ClaimTypes.Sid, user.user_code));
        claimIdentity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.user_code));
        claimIdentity.AddClaim(new Claim(ClaimTypes.Name, user.user_name));

        var claimsPrincipal = new ClaimsPrincipal(claimIdentity);

        context.HttpContext.User = claimsPrincipal;

        base.OnActionExecuting(context);
    }

    private TokenUserInfoVo VerificationToken(string token)
    {
        TokenUserInfoVo userInfo = null;
        JYHttpClient client = new JYHttpClient((int)EnumContentType.json, InterfaceUrl.userinfo, "", token);
        string userInfoJson = client.ExecuteGet();

        if (!string.IsNullOrEmpty(userInfoJson))
        {
            userInfo = JsonConvert.DeserializeObject<TokenUserInfoVo>(userInfoJson);
        }
        return userInfo;
    }
}

*
*
*
*

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值