Ali
Ali

Reputation: 437

How i can perform Token based Authentication in SignalR?

I am using signalR in asp.net mvc application,I want to authenticate cross domain clients by token based authentication.I did not found complete solution for it.

 app.Map("/signalr", map =>
    {
        map.UseCors(CorsOptions.AllowAll);

        map.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
        {
            Provider = new QueryStringOAuthBearerProvider()
        });

        var hubConfiguration = new HubConfiguration
        {
            Resolver = GlobalHost.DependencyResolver,
        };
        map.RunSignalR(hubConfiguration);
    });

    public class QueryStringOAuthBearerProvider : OAuthBearerAuthenticationProvider
    {
        public override Task RequestToken(OAuthRequestTokenContext context)
        {
            var value = context.Request.Query.Get("access_token");

            if (!string.IsNullOrEmpty(value))
            {
                context.Token = value;
            }

            return Task.FromResult<object>(null);
        }
    }

    public class impAuthHub : Hub
    {
        [Authorize]
        public void SendMessage(string name, string message)
        {
           Clients.All.newMessage(name, message);
        }
    }

I dont know how i will get token to pass query string to my startup class?

Upvotes: 0

Views: 2152

Answers (1)

PrathapG
PrathapG

Reputation: 789

You will be needed to use OAuth Bearer Token authentication with SignalR. and you need to use Microsoft’s OWIN Security and ASP.NET Identity libraries then include the WebAPI and Individual Accounts security options. This is a Full- Demo

Please find the code base for working sample git , which will help you.

Upvotes: 1

Related Questions