loading...
Cover image for SignalR core python client (II): Authentication

SignalR core python client (II): Authentication

mandrewcito profile image Andrés Baamonde Lozano Updated on ・2 min read

Intro

As i introduced on my previous post, next step on my library was authentication on SignalR Core Hubs. A good guide and example of this authentication is this: aspnet docs. You can donwload fully working example on their github.

Once introduction is finished, let's go to the playground:

Server side

Quick fix, memory database

Creating a database on disk for this example ... no thanks

Go to Startup.cs, comment UseSqlServer and add UseInMemoryDatabase

...
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(options =>
               //  options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")
            options.UseInMemoryDatabase()   
        );
...

Create user on the web

Register

Client side

First, get auth token

Post request of this example is a form-data, response is a JSON.

def signalr_core_example_login(url, user, username_password):
    response = requests.post(url, data={"email": user, "password": username_password})
    return response.json()["token"]

Build connection and add signalr event handlers

token = signalr_core_example_login(login_url, username, password)
hub_connection = HubConnection(
    server_url,
    token=token,
    negotiate_headers={"Authorization": "Bearer " + token})

hub_connection.build()
hub_connection.on("ReceiveSystemMessage", print)
hub_connection.on("ReceiveChatMessage", print)
hub_connection.on("ReceiveDirectMessage", print)
hub_connection.start()

Now you have connection initialized and you can send messages auth through signalr hubs.
Fully example

Future

  • message pack
  • Auth (now only working by querystring negotiate)

Links

Github
Pypi

I'm trying to build this library with simplest way possible, so if you think that there is a better way to do it, leave a comment. Asap i'll edit github library contribute section, so any contribution is welcommed. There is a lot of work to do with these, messagepack. streams ..

Thank you for reading, and write any thought below :D

Posted on by:

Discussion

pic
Editor guide