我有以下方法:
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (Composite.C1Console.Security.UserValidationFacade.IsLoggedIn())
SetPrincipal(request, new ClaimsPrincipal(new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Role, "Administrator") },)));
var test = request.GetClaimsPrincipal();
return base.SendAsync(request, cancellationToken);
}
我的问题是,如果我检查test.Identity.IsAuthenticated
尚未设置为true。这只是一些测试代码来弄清楚如何。我错过了什么。
答案 0 :(得分:8)
您需要在ClaimsIdentity ctor中设置身份验证类型。
答案 1 :(得分:4)
您需要为ClaimsIdentity
构造函数指定一个ClaimsPrincipal
实例,指定authenticationType
,例如“Basic”。索赔可以是null
。
var principal = new ClaimsPrincipal(new ClaimsIdentity(null, "Basic"));
var isAuthenticated = principal.Identity.IsAuthenticated; // true
答案 2 :(得分:0)
if (Composite.C1Console.Security.UserValidationFacade.IsLoggedIn())
SetPrincipal(request, new ClaimsPrincipal(new ClaimsIdentity(new Claim[] {
new Claim(ClaimTypes.Role, "Administrator"),
new Claim(ClaimTypes.NameIdentifier, UserValidationFacade.GetUsername())}, "Basic")));
var test = request.GetClaimsPrincipal();
return base.SendAsync(request, cancellationToken);