2017-03-07 20 views
3

I AspNetCore und die Cookie-Middleware für die Authentifizierung verwenden, die ich so einstellen ...SignalR Autorisieren Attribut funktioniert nicht mit Cookie-Authentifizierung

app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
    AuthenticationScheme = Constants.AuthenticationScheme, 
    AutomaticAuthenticate = true, 
    AutomaticChallenge = true, 
    LoginPath = new PathString("/") 
}); 

ich erfolgreich bin Authentifizierung eines Anmeldeformular und dann umleiten zu ein Controller, der mit einem [Authorize] -Attribut gekennzeichnet ist.

Der Controller lädt dann eine Seite mit dem Javascript-SignalR-Client, der eine Verbindung zum Hub herstellt.

Allerdings, wenn ich das signalR [Authorize] -Attribut hinzufügen. Ich bekomme ein 401 Unautorisiertes vom Server.

Wie kann ich signalR das Authentifizierungs-Cookie erkennen lassen? Ich kann sehen, dass das Cookie in Context.RequestCookies übergeben wurde.

Fehlgeschlagen, wie könnte ich manuell den Cookie entschlüsseln und den Benutzer selbst setzen?

Antwort

1

Ich habe es gelöst, indem ich den Cookie selbst entschlüsselt habe, aber ich wäre sehr an einem besseren Weg interessiert.

Ich habe das Authentifizierungsticket-Format mit Hilfe von diesem Question

public static class SecurityHelper 
{ 
    /// <summary> 
    /// Gets the format for the security cookie used to encrypt and decrpyt cookie 
    /// </summary> 
    /// <param name="services">The app service provider</param> 
    /// <returns>The authentication ticket format</returns> 
    public static ISecureDataFormat<AuthenticationTicket> GetTicketFormat(IServiceProvider services) 
    { 
     var authenticationType = "Cookies"; 
     var protectionProvider = services.GetRequiredService<IDataProtectionProvider>(); 
     var dataProtector = protectionProvider.CreateProtector("Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware", authenticationType, "v2"); 
     return new TicketDataFormat(dataProtector); 
    } 
} 

und sagte dem Cookie-Middleware für die Erzeugung dieses Ticket-Format in meinem startup.cs Klasse zu verwenden ... eine Hilfsklasse

app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
      AuthenticationScheme = "Cookies", 
      AutomaticAuthenticate = true, 
      AutomaticChallenge = true, 
      LoginPath = new PathString("/"), 
      TicketDataFormat = SecurityHelper.GetTicketFormat(app.ApplicationServices) 
}); 

Und dann mit Hilfe von this Artikel erstellt mein eigenes Signal-Authentifizierungs-Attribut zum Entschlüsseln des Cookies und setzen Sie den Benutzer-Prinzipal.

/// <summary> 
/// Attribute to have signalr hubs authenticate 
/// </summary> 
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)] 
public class AuthorizeHubUsersAttribute : AuthorizeAttribute 
{ 
    /// <summary> 
    /// Decrpyt the authentication cookie and set the user principal 
    /// </summary> 
    /// <param name="hubDescriptor">The hub descriptor</param> 
    /// <param name="request">The request object</param> 
    /// <returns>If the user is aythenticated</returns> 
    public override bool AuthorizeHubConnection(HubDescriptor hubDescriptor, HttpRequest request) 
    { 
     var cookie = request.Cookies[".AspNetCore.Cookies"]; 
     var ticketFormat = SecurityHelper.GetTicketFormat(request.HttpContext.RequestServices); 
     var authenticationTicket = ticketFormat.Unprotect(cookie); 
     request.HttpContext.User = authenticationTicket.Principal; 

     return base.AuthorizeHubConnection(hubDescriptor, request); 
    } 

    /// <summary> 
    /// Check the user is authenticated 
    /// </summary> 
    /// <param name="user">The user principal</param> 
    /// <returns>If the user is aythenticated</returns> 
    protected override bool UserAuthorized(IPrincipal user) 
    { 
     if (user?.Identity == null) 
     { 
      return false; 
     } 
     return user.Identity.IsAuthenticated; 
    } 
} 

Dann alles, was ich tun musste, war meine Hub-Klasse mit dem

[AuthorizeHubUsersAttribute()] 
public class GameMessageHub : Hub<IGameMessageHub> { 
.... 
} 

Attribute schmückt ich keine Möglichkeit der Injektion von Abhängigkeiten in ein signalr Attribut finden konnte, aber wenn jemand Zahlen es mir aus Ich würde es gerne wissen.

Verwandte Themen