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

3
我一直在尝试在我的SignalR服务器上实现自动Windows身份验证,只能在C#客户端中使用,但不能在JavaScript Web客户端中使用。
为了给您一些背景:我有一个C#服务器,提供对SignalR API(Hub)的访问,并向其连接的客户端广播消息。我有两个版本的该服务器,一个是独立的自托管版本,另一个是包含在ASP Web解决方案中的版本,它们共享相同的Owin启动代码。
我还有两个客户端:一个是独立的C#客户端,在其中可以创建一个并将其属性设置为(这很好用),以及由我的ASP服务器生成的JavaScript SignalR客户端(js hub proxy)。
当尝试使用SignalR JavaScript客户端连接时,我遇到了来自我的ASP服务器的未经授权的访问响应。
我一直在努力寻找任何关于JavaScript等效于设置的属性的文档。

工作设置:

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

客户端 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。

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

您有关于如何实现这一点的任何想法吗?(我已经至少两次阅读了所有的signalr文档...)

1个回答

2
最终,我意识到我的代码没有问题。我只是通过更改本地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<anonymousAuthentication enabled="false">

然后我将<windowsAuthentication enabled="false">更改为<windowsAuthentication enabled="true">
感谢如何在ASP.NET开发服务器上启用Windows身份验证?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接