简体   繁体   English

如何获取使用带有 owin 身份验证的客户端 mvc 应用程序的 SignalR 连接用户的身份?

[英]How to get identity of SignalR connected users which are using client mvc application with owin authentication?

In my solution, I have 2 projects : SignalRServer , WebClient which are both MVC application .在我的解决方案中,我有 2 个项目: SignalRServerWebClient ,它们都是MVC 应用程序 In my server, I've used SignalR 2 Hub and in client project I've used jquery SignalR library to connect to hub server by passing hosted server url as hub connection url.在我的服务器中,我使用了SignalR 2 集线器,在客户端项目中,我使用了jquery SignalR库通过将托管服务器 url 作为集线器连接 url 传递来连接到集线器服务器。

Everything is working as well and client can receive messages from server.一切正常,客户端可以从服务器接收消息。

The problem : In client project I have added authentication and users must enter their username and password to view the page which messages are going to be viewed.问题:在客户端项目中,我添加了身份验证,用户必须输入他们的用户名和密码才能查看将要查看的消息的页面。 Now I want to get the signed in username which has connected to SignalR Hub Server and send related messages to each user using this username.现在我想获取已连接到SignalR Hub 服务器的登录用户名,并使用此用户名向每个用户发送相关消息。

How do I know which user has connected to server while the authentication is processing in client project separated from server project ?在与服务器项目分开的客户端项目中处理身份验证时,我如何知道哪个用户已连接到服务器?

JQuery client hub: JQuery 客户端中心:

var PbxHub = $.connection.pbxhub;

$.connection.hub.url = "http://localhost:10437/signalr";
$.connection.hub.logging = true;

$.connection.hub.start({
    jsonp: true,
    withCredentials: false
}).done(function () {
    console.log('Hub is connected.');
});

$.connection.hub.disconnected(function () {
    console.log('Hub is disconnected.');
    setTimeout(function () {
        $.connection.hub.start();
    }, 1500); // Restart connection after 1500 miliseconds.
});

Client Startup:客户端启动:

public void Configuration(IAppBuilder app)
{
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Home/Login"),
    });
}

Server Startup:服务器启动:

    public void Configuration(IAppBuilder app)
    {
        app.Map("/signalr", map =>
        {
            var hubConfiguration = new HubConfiguration
            {
                EnableJavaScriptProxies = true,
                EnableJSONP = true,
                EnableDetailedErrors = true
            };

            map.UseCors(CorsOptions.AllowAll);
            map.RunSignalR(hubConfiguration);
        });
    }

Appreciated for any idea.感谢任何想法。

As described by Microsoft documentation at here https://docs.microsoft.com/en-us/aspnet/core/signalr/authn-and-authz?view=aspnetcore-3.1#cookie-authentication如此处的 Microsoft 文档所述https://docs.microsoft.com/en-us/aspnet/core/signalr/authn-and-authz?view=aspnetcore-3.1#cookie-authentication

In a browser-based app, cookie authentication allows your existing user credentials to automatically flow to SignalR connections.在基于浏览器的应用程序中,cookie 身份验证允许您现有的用户凭据自动流到 SignalR 连接。 When using the browser client, no additional configuration is needed.使用浏览器客户端时,无需额外配置。 If the user is logged in to your app, the SignalR connection automatically inherits this authentication.如果用户登录到您的应用,SignalR 连接会自动继承此身份验证。

So, by using same DBContext which used in client app , I could access to connectionId of users which connected to hub and either insert records to database after sending them messages.因此,通过使用在客户端应用程序中使用的相同 DBContext,我可以访问连接到集线器的用户的 connectionId,并在向他们发送消息后将记录插入数据库。

    if (_context.Extension.Any(ex=>ex.Code.ToString() == deviceName))
    {
        extensionID = _context.Extension.SingleOrDefault(ex => ex.Code.ToString() == deviceName).ID.ToString();
    }
    else { return; }

    asterHub.Clients.User(extensionID).SendState(
            e.Device_state.Name,
            string.Format("{0}:{1}:{2}",
                pc.GetHour(DateTime.Now),
                pc.GetMinute(DateTime.Now),
                pc.GetSecond(DateTime.Now)),
            state,
            stateCode);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM