18

I have the following method:

        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);
        }

my problem is that if i inspect the test.Identity.IsAuthenticated is has not been set to true. This is just some test code to figure out how. What am I missing.

Poul K. Sørensen
  • 16,950
  • 21
  • 126
  • 283

3 Answers3

20

You need to specify a ClaimsIdentity instance to the ClaimsPrincipal constructor that specifies a authenticationType such as "Basic". Claims can be null.

var principal = new ClaimsPrincipal(new ClaimsIdentity(null, "Basic"));
var isAuthenticated = principal.Identity.IsAuthenticated; // true
Fred
  • 12,086
  • 7
  • 60
  • 83
15

You need to set an authentication type in the ClaimsIdentity ctor.

leastprivilege
  • 18,196
  • 1
  • 34
  • 50
3
        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);
Poul K. Sørensen
  • 16,950
  • 21
  • 126
  • 283