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

时间:2013-07-02 12:38:11

标签: 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);
        }

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

3 个答案:

答案 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);