Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add a query string to the connection? #9

Closed
coffeecoding opened this issue Sep 4, 2021 · 3 comments
Closed

How to add a query string to the connection? #9

coffeecoding opened this issue Sep 4, 2021 · 3 comments

Comments

@coffeecoding
Copy link

I want my server to identify me, for example by my username. For that I need to pass my username when opening a connection, how to do that?

@golovin-igor
Copy link

golovin-igor commented Oct 10, 2021

You can use accessTokenFactory, that adds token parameter to the query:

void createHubConnection() async {
      final httpConnectionOptions = new HttpConnectionOptions(
          accessTokenFactory: () async => 'put_some_token_here',
          logMessageContent: false);

      _hubConnection = HubConnectionBuilder()
          .withUrl(kHubConnectionURL, options: httpConnectionOptions);

      await _hubConnection.start();
     _hubConnection.invoke("tokenTest", args: <Object>[]);
}

then on server you will be able to retrieve token as following:

[HubMethodName("tokenTest")]
public async Task<string> TokenTest()
{
   var request = Context.GetHttpContext().Request;
   if (request.Query.ContainsKey("access_token"))
   {
            string token = request.Query["access_token"].FirstOrDefault();
            //check user token here
   }
}

@magnuswikhog
Copy link

Is it possible to do this during the initial connection instead of invoking a method after the connection has already been established?

@coffeecoding
Copy link
Author

coffeecoding commented Sep 8, 2022

@magnuswikhog
I suppose you use the args array in

await _hubConnection.start();
_hubConnection.invoke("tokenTest", args: <Object>[]);

Does that suffice for your purpose?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants