asp.net - How can I authorize a SignalR Hub using Azure Mobile Apps Authentication -


i have mobile app using facebook auth through azure. auth works fine apicontrollers [mobileapicontroller] flag.

i can't seem find how make signalr hub authorize - authorize attribute blocks access users. articles have found seem old , using deprecated azure mobile services different , not compatible.

i have configured signalr client connect long-polling x-zumo-auth header set.

i ended making custom signalr authentication attribute. code below else interested.

  public class hubauthorizeattribute : authorizeattribute   {     public hubauthorizeattribute()     {     }      public override bool authorizehubconnection(hubdescriptor hubdescriptor, irequest request)     {         var owincontext = request.gethttpcontext().getowincontext();          claimsprincipal claimsprincipalfromtoken;         var options = startup.authenticationoptions;          string tokenfromheader = request.headers[appserviceauthenticationhandler.authenticationheadername];         if (!string.isnullorempty(tokenfromheader))         {             bool claimsarevalid = options.tokenhandler.tryvalidatelogintoken(tokenfromheader, options.signingkey, options.validaudiences, options.validissuers, out claimsprincipalfromtoken);             if (claimsarevalid)             {                 var identity = claimsprincipalfromtoken.identity claimsidentity;                  request.environment["server.user"] = new claimsprincipal(identity);                 return true;             }         }          return false;     }      public override bool authorizehubmethodinvocation(ihubincominginvokercontext hubincominginvokercontext, bool appliestomethod)     {         var connectionid = hubincominginvokercontext.hub.context.connectionid;         // check authenticated user principal environment         var environment = hubincominginvokercontext.hub.context.request.environment;         var principal = environment["server.user"] claimsprincipal;         if (principal != null && principal.identity != null && principal.identity.isauthenticated)         {             // create new hubcallercontext instance principal generated token             // , replace current context in hubs can retrieve current user identity             hubincominginvokercontext.hub.context = new hubcallercontext(new serverrequest(environment), connectionid);             return true;         }         else         {             return false;         }     } } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -