2013-07-02 6 views
6

Ho il seguente metodo:Come creare un ClaimsPrincipal con Identity.Authenticated impostato su true?

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

il mio problema è che se ho ispezionare le test.Identity.IsAuthenticated è non è stata impostata su true. Questo è solo un codice di prova per capire come. Cosa mi manca

risposta

7

È necessario impostare un tipo di autenticazione nel ClaimsIdentity ctor.

+0

calcolato 5min fa: D Grazie –

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

È necessario specificare un'istanza ClaimsIdentity per il costruttore ClaimsPrincipal che specifica un authenticationType come "Base". Le richieste possono essere null.

var principal = new ClaimsPrincipal(new ClaimsIdentity(null, "Basic")); 
var isAuthenticated = principal.Identity.IsAuthenticated; // true