Claim ClaimsIdentity ClaimsPrincipal 筛选器 过滤器

编程入门 行业动态 更新时间:2024-10-07 20:29:33

Claim ClaimsIdentity ClaimsPrincipal 筛选器 <a href=https://www.elefans.com/category/jswz/34/1771166.html style=过滤器"/>

Claim ClaimsIdentity ClaimsPrincipal 筛选器 过滤器

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);//okvar claimIdentity = new ClaimsIdentity("Cookie");//okclaimIdentity.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,//持久CookieExpiresUtc = 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;}
}

*
*
*
*

更多推荐

Claim ClaimsIdentity ClaimsPrincipal 筛选器 过滤器

本文发布于:2024-02-06 19:43:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1751119.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   Claim   ClaimsIdentity   ClaimsPrincipal

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!