简体   繁体   English

如何在 SignalR javascript 客户端上使用 Windows 身份验证

[英]How to use Windows Authentication on the SignalR javascript client

I've been trying to implement automatic Windows Authentication on my signalr server, and only got it working with a C# client, but not with the javascript web client.我一直在尝试在我的信号服务器上实现自动 Windows 身份验证,并且只让它与 C# 客户端一起工作,而不是与 javascript web 客户端一起工作。

To give you some context: I have a C# server that provides an access to a SignalR API (Hub), and broadcasts messages to its connected clients.给你一些上下文:我有一个 C# 服务器,它提供对 SignalR API(集线器)的访问,并向其连接的客户端广播消息。 I have two versions of that server, one standalone self-host and one included in an ASP web solution, that share the same Owin Startup code.我有该服务器的两个版本,一个是独立的自托管,另一个包含在 ASP Web 解决方案中,它们共享相同的 Owin 启动代码。

I also got two clients : one standalone C# one, in which I can create an IHubConnection and set its Credentials property to CredentialCache.DefaultCredentials (which works fine), and also the javascript signalr client (js hub proxy) generated by my ASP server.我还有两个客户端:一个独立的 C# 一个,我可以在其中创建一个IHubConnection并将其Credentials属性设置为CredentialCache.DefaultCredentials (工作正常),以及由我的 ASP 服务器生成的 javascript 信号客户端(js 集线器代理)。

I'm facing an Unauthorized Access response from my ASP server when trying to connect using the signalr javascript client.尝试使用信号器 javascript 客户端进行连接时,我正面临来自 ASP 服务器的未经授权的访问响应。

I've been struggling to find any documentation about a javascript equivalent to setting the Credentials property of my HubConnection .我一直在努力寻找有关与设置我的HubConnectionCredentials属性等效的 javascript 的任何文档。


The working setup:工作设置:

The Owin Startup class: Owin 启动类:

public class ServerStartup
{
    public virtual HubConfiguration HubConfiguration => new HubConfiguration
    {
        EnableDetailedErrors = true,
        EnableJavaScriptProxies = false
    };

    public void Configuration(IAppBuilder app)
    {
        ConfigureSignalR(app);
        ConfigureAuth(app);
    }

    private void ConfigureSignalR(IAppBuilder app)
    {
        app.UseCors(CorsOptions.AllowAll);
        try
        {
            app.MapSignalR("/endpoint", HubConfiguration);
        }
        catch (InvalidOperationException)
        {
            // raised when the system's performance counter is not available
        }
    }

    private void ConfigureAuth(IAppBuilder app)
    {
        object listener;
        if (app.Properties.TryGetValue(typeof(HttpListener).FullName, out listener))
        {
            ((HttpListener)listener).AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;
        }
    }
}

The Client HubConnection:客户端集线器连接:

var connection = new ClientConnection($"http://localhost:{Port}/endpoint", useDefaultUrl: false)
{
    TransportConnectTimeout = TimeSpan.FromSeconds(50),
    Credentials = CredentialCache.DefaultCredentials
};

I get the expected result that my Context.User contains the right IIdentity filled with information from the local windows user currently connected.我得到了预期的结果,即我的Context.User包含正确的IIdentity填充了来自当前连接的本地 Windows 用户的信息。


The failing setup: 失败的 设置:

EDIT: It actually works, see my answer below.编辑:它确实有效,请参阅下面的答案。

My ASP Startup class:我的 ASP 启动类:

[assembly: OwinStartup(typeof(Dashboard.Startup))]
namespace Dashboard
{
    public class Startup : ServerStartup
    {
        public override HubConfiguration HubConfiguration => new HubConfiguration
        {
            EnableDetailedErrors = true,
            EnableJavaScriptProxies = true,
            EnableJSONP = true
        };
    }
}

The javascript client: javascript客户端:

$.connection.hub.start()
    .done(function(  ) {
        $.connection.MyHubName.server.myApiMethod(null)
            .done(function (result) {
                // success
            } )
            .fail(function (error) {
                console.log(error);
                options.error(error);
            } );
    })
    .fail(function (error) {
        console.log(error);
        options.error(error);
    });

I don't know how I could tweak my javascript client so that the windows credentials are passed to the SignalR API.我不知道如何调整我的 javascript 客户端,以便将 Windows 凭据传递给 SignalR API。

The feature I want to implement is that anyone from my local internal network is automatically logged in with his/her Windows Credentials when getting to the ASP website from a web browser, so that I can identify them from my SignalR Hub.我想要实现的功能是,当从 Web 浏览器访问 ASP 网站时,我本地内部网络中的任何人都会使用他/她的 Windows 凭据自动登录,以便我可以从我的 SignalR 集线器中识别它们。

Any idea about how I can implement that?关于如何实现它的任何想法? (I already read all the signalr documentation... at least twice...) (我已经阅读了所有的信号器文档......至少两次......)

In the end, I realized there is nothing wrong with my code.最后,我意识到我的代码没有任何问题。 I just fixed the issue by changing the settings of my local IIS Express server to disallow anonymous authentication and allow windows authentication.我只是通过更改本地 IIS Express 服务器的设置来禁止匿名身份验证并允许 Windows 身份验证来解决该问题。

I had to change the configuration in .vs/config/applicationhost.config :我不得不更改.vs/config/applicationhost.config

<security>
    <access sslFlags="None" />
    <applicationDependencies>
        <application name="Active Server Pages" groupId="ASP" />
    </applicationDependencies>
    <authentication>
        <anonymousAuthentication enabled="false" userName="" />
        <basicAuthentication enabled="false" />
        <clientCertificateMappingAuthentication enabled="false" />
        <digestAuthentication enabled="false" />
        <iisClientCertificateMappingAuthentication enabled="false" />
        <windowsAuthentication enabled="true">
            <providers>
                <add value="Negotiate" />
                <add value="NTLM" />
            </providers>
        </windowsAuthentication>
    </authentication>
    <!-- ... -->
</security>

I checked that anonymousAuthentication was set to false : <anonymousAuthentication enabled="false">我检查了anonymousAuthentication设置为false<anonymousAuthentication enabled="false">

Then I changed <windowsAuthentication enabled="false"> to <windowsAuthentication enabled="true"> .然后我将<windowsAuthentication enabled="false">更改为<windowsAuthentication enabled="true">

Thanks to How to enable Windows Authentication on ASP.NET Development Server?感谢如何在 ASP.NET 开发服务器上启用 Windows 身份验证?

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

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