How to create a ClaimsPrincipal formula with Identity.Authenticated identifier set to true?

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 check that the test.Identity.IsAuthenticated is parameter is not set to true. This is just some test code to figure out how to do this. What am I missing.

+6
source share
3 answers

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

+7
source

You need to specify a ClaimsIdentity instance for the ClaimsPrincipal constructor that specifies authenticationType , for example, "Basic". Claims may be null .

 var principal = new ClaimsPrincipal(new ClaimsIdentity(null, "Basic")); var isAuthenticated = principal.Identity.IsAuthenticated; // true 
+2
source
  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); 
0
source

Source: https://habr.com/ru/post/948540/


All Articles