如何创建一个Identity.Authenticated设置为true的ClaimsPrincipal?

Pou*_*sen 9 claims-based-identity

我有以下方法:

        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);
        }
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果我检查test.Identity.IsAuthenticated是没有设置为真.这只是一些测试代码来弄清楚如何.我错过了什么

lea*_*ege 10

您需要在ClaimsIdentity ctor中设置身份验证类型.


Fre*_*red 8

您需要为构造函数指定一个ClaimsIdentity实例,该实例ClaimsPrincipal指定一个authenticationType诸如“ Basic” 的实例。索赔可以null

var principal = new ClaimsPrincipal(new ClaimsIdentity(null, "Basic"));
var isAuthenticated = principal.Identity.IsAuthenticated; // true
Run Code Online (Sandbox Code Playgroud)