【发布时间】:2016-08-22 06:55:42
【问题描述】:
设置:
- 创建一个包含一堆声明的
ClaimsPrincipal。 - 致电
httpContext.Authentication.SignInAsync("MyAuthScheme", myClaimsPrincipal, myAuthProperties) - 尝试遍历
httpContext.User.Claims上的所有声明。
结果:
来自 pt 的声明。 1 不在httpContext.User.Claims-collection 中。
为什么?
编辑:
var claims = GetPersistentClaims(username.ToLowerInvariant());
var identity = new ClaimsIdentity(claims, AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);
var authenticationManager = _httpContextAccessor.HttpContext.Authentication;
var properties = new AuthenticationProperties {IsPersistent = isPersistent};
await authenticationManager.SignInAsync(AuthenticationScheme, principal, properties);
// This is where I inspect _httpContextAccessor.HttpContext.User.Claims.
【问题讨论】:
-
httpContext.User.Identity.IsAuthenticated的值是多少? -
@adem caglin 这是
false。 -
您在何时何地迭代
httpContext.User.Claims?如果可能的话,你能发布你的代码吗? -
我已经添加了源代码sn-p。
-
iirc 它们在登录后 的请求中可用,因此您需要在使用
SignInAsync登录后将用户重定向到新的/受保护的资源,因为在您调用SignInAsync时,已经设置了 HttpContext 并且User属性在下一次请求之前不会更改
标签: asp.net-core claims-based-identity