【问题标题】:How to use Windows Authentication on the SignalR javascript client如何在 SignalR javascript 客户端上使用 Windows 身份验证
【发布时间】:2017-03-27 13:03:25
【问题描述】:

我一直在尝试在我的信号器服务器上实现自动 Windows 身份验证,但只能使用 C# 客户端,但不能使用 javascript Web 客户端。

为您提供一些背景信息:我有一个 C# 服务器,它提供对 SignalR API (Hub) 的访问,并向其连接的客户端广播消息。我有该服务器的两个版本,一个是独立的自托管服务器,一个包含在 ASP Web 解决方案中,它们共享相同的 Owin 启动代码。

我还有两个客户端:一个是独立的 C# 客户端,我可以在其中创建一个 IHubConnection 并将其 Credentials 属性设置为 CredentialCache.DefaultCredentials(工作正常),以及 javascript signalr 客户端(js hub 代理) 由我的 ASP 服务器生成。

在尝试使用 signalr javascript 客户端进行连接时,我正面临来自 ASP 服务器的未经授权的访问响应。

我一直在努力寻找任何有关设置我的HubConnection 的Credentials 属性的javascript 文档。


工作设置:

Owin Startup 类:

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;
        }
    }
}

客户端 HubConnection:

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

我得到预期的结果,我的Context.User 包含正确的IIdentity,其中填充了来自当前连接的本地 Windows 用户的信息。


失败设置:

编辑:它确实有效,请参阅下面的答案。

我的 ASP 启动课程:

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

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);
    });

我不知道如何调整我的 javascript 客户端,以便将 Windows 凭据传递给 SignalR API。

我想要实现的功能是,当我从网络浏览器访问 ASP 网站时,我本地内部网络中的任何人都会使用他/她的 Windows 凭据自动登录,以便我可以从我的 SignalR Hub 中识别他们。

知道如何实现它吗? (我已经阅读了所有的信号器文档......至少两次......)

【问题讨论】:

    标签: javascript c# asp.net authentication signalr


    【解决方案1】:

    最后,我意识到我的代码没有任何问题。 我刚刚通过更改本地 IIS Express 服务器的设置以禁止匿名身份验证并允许 Windows 身份验证来解决此问题。

    我不得不更改.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>
    

    我检查了 anonymousAuthentication 是否设置为 false :&lt;anonymousAuthentication enabled="false"&gt;

    然后我将&lt;windowsAuthentication enabled="false"&gt; 更改为&lt;windowsAuthentication enabled="true"&gt;。

    感谢How to enable Windows Authentication on ASP.NET Development Server?

    【讨论】:

      猜你喜欢
      • 2014-02-07
      • 2020-06-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-12
      • 2014-08-07
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多