2016-08-29 4 views
0

Fehler beim Aufruf des "SignalR: Aufrufer ist nicht berechtigt, die LoadPvtChatHistory_Hub-Methode aufrufen" auch IsAuthenticated wird immer falsch.SignalR: Der Aufrufer ist nicht berechtigt, die Methode aufzurufen

public class QueryStringBearerAuthorizeAttribute : AuthorizeAttribute 
{ 
    public override bool AuthorizeHubConnection(Microsoft.AspNet.SignalR.Hubs.HubDescriptor hubDescriptor, IRequest request) 
    { 
     var dataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider(); 
     var secureDataFormat = new Microsoft.Owin.Security.DataHandler.TicketDataFormat(dataProtectionProvider.Create()); 

     //var secureDataFormat = new Microsoft.Owin.Security.DataHandler.TicketDataFormat(new MachineKeyProtector()); 
     var token = request.QueryString.Get("Bearer"); 

     if (string.IsNullOrEmpty(token)) 
      return false; 
    // var token = request.QueryString.Get(WebApiConfig.AuthenticationType); 
     var authenticationTicket = secureDataFormat.Unprotect(token); 

     if (authenticationTicket == null || authenticationTicket.Identity == null || !authenticationTicket.Identity.IsAuthenticated) 
     { 
      return false; 
     } 

     request.Environment["server.User"] = new ClaimsPrincipal(authenticationTicket.Identity); 
     request.Environment["server.Username"] = authenticationTicket.Identity.Name; 
     request.GetHttpContext().User = new ClaimsPrincipal(authenticationTicket.Identity); 
     return true; 
    } 

    public override bool AuthorizeHubMethodInvocation(Microsoft.AspNet.SignalR.Hubs.IHubIncomingInvokerContext hubIncomingInvokerContext, bool appliesToMethod) 
    { 
     var connectionId = hubIncomingInvokerContext.Hub.Context.ConnectionId; 
     // check the authenticated user principal from environment 
     var environment = hubIncomingInvokerContext.Hub.Context.Request.Environment; 
     var principal = environment["server.User"] as System.Security.Claims.ClaimsPrincipal; 
     if (principal != null && principal.Identity != null && principal.Identity.IsAuthenticated) // IsAuthenticated always gets false 
     { 
      // create a new HubCallerContext instance with the principal generated from token 
      // and replace the current context so that in hubs we can retrieve current user identity 
      hubIncomingInvokerContext.Hub.Context = new Microsoft.AspNet.SignalR.Hubs.HubCallerContext(new Microsoft.AspNet.SignalR.Owin.ServerRequest(environment), connectionId); 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

} 

wenn Klick auf Senden-Button ich diese Methode Invoke von AppHub nenne aber es gibt mir einen Fehler wie nicht autorisiert, während fordern.

AppHub.invoke('LoadPvtChatHistory_Hub', curVM, GENERAL_CONFIG.APP_messageExpiryTimeLimit); 

AppHub.cs

[QueryStringBearerAuthorize] 
    public class AppHub : Hub 
    { 
public void LoadPvtChatHistory_Hub(string xiSelectedUserID, int xiMessageExpiryTimeLimit) 
     { 
     ///code.... 
     } 
    } 

[16:36:33 GMT+0530 (India Standard Time)] SignalR: Caller is not authorized to invoke the LoadPvtChatHistory_Hub method on AppHub. 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNet.SignalR.Hubs.HubPipelineModule.<>c__DisplayClass0_0.<<BuildIncoming>b__0>d.MoveNext(). 
jquery.signalR.js:82 [16:36:33 GMT+0530 (India Standard Time)] SignalR: apphub.loadPvtChatHistory_Hub failed to execute. Error: Caller is not authorized to invoke the LoadPvtChatHistory_Hub method on AppHub. 
jquery.signalR.js:82 [16:37:00 GMT+0530 (India Standard Time)] SignalR: Invoking apphub.sendPrivateMessage_Hub 
jquery.signalR.js:82 [16:37:04 GMT+0530 (India Standard Time)] SignalR: Caller is not authorized to invoke the SendPrivateMessage_Hub method on AppHub. 
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
    at Microsoft.AspNet.SignalR.Hubs.HubPipelineModule.<>c__DisplayClass0_0.<<BuildIncoming>b__0>d.MoveNext(). 
jquery.signalR.js:82 [16:37:04 GMT+0530 (India Standard Time)] SignalR: apphub.sendPrivateMessage_Hub failed to execute. Error: Caller is not authorized to invoke the SendPrivateMessage_Hub method on AppHub. 
+0

das gleiche Problem bekommen. – Sutirth

Antwort

0

Hier ist meine Lösung, die Arbeit an Azure und lokaler Ebene. AngularJS, Web API und SignalR request.Environment ["server.User"] Dieser Code funktioniert nicht auf Azure. Zuerst erstelle ich meine benutzerdefinierte Filterklasse.

public override bool AuthorizeHubMethodInvocation(IHubIncomingInvokerContext hubIncomingInvokerContext, bool appliesToMethod) 
    { 
     var connectionId = hubIncomingInvokerContext.Hub.Context.ConnectionId; 
     var request=hubIncomingInvokerContext.Hub.Context.Request; 
     var _Authorization = request.QueryString.Get("Bearer"); 
     if (!string.IsNullOrEmpty(_Authorization)) 
     { 
      //var token = _Authorization.Replace("Bearer ", ""); 
      var ticket = Startup.OAuthOptions.AccessTokenFormat.Unprotect(_Authorization); 

      if (ticket != null && ticket.Identity != null && ticket.Identity.IsAuthenticated) 
      { 
       Dictionary<string, object> _DCI = new Dictionary<string, object>(); 
       _DCI.Add("server.User", new ClaimsPrincipal(ticket.Identity)); 
       hubIncomingInvokerContext.Hub.Context = new HubCallerContext(new ServerRequest(_DCI), connectionId); 
       return true; 
      } 
     } 
     return false; 
    } 

Refer this article.

Verwandte Themen