过滤器"/>
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 筛选器 过滤器
发布评论