[英]How to use Windows Authentication on the SignalR javascript client
我一直在尝试在我的信号服务器上实现自动 Windows 身份验证,并且只让它与 C# 客户端一起工作,而不是与 javascript web 客户端一起工作。
给你一些上下文:我有一个 C# 服务器,它提供对 SignalR API(集线器)的访问,并向其连接的客户端广播消息。 我有该服务器的两个版本,一个是独立的自托管,另一个包含在 ASP Web 解决方案中,它们共享相同的 Owin 启动代码。
我还有两个客户端:一个独立的 C# 一个,我可以在其中创建一个IHubConnection
并将其Credentials
属性设置为CredentialCache.DefaultCredentials
(工作正常),以及由我的 ASP 服务器生成的 javascript 信号客户端(js 集线器代理)。
尝试使用信号器 javascript 客户端进行连接时,我正面临来自 ASP 服务器的未经授权的访问响应。
我一直在努力寻找有关与设置我的HubConnection
的Credentials
属性等效的 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;
}
}
}
客户端集线器连接:
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 集线器中识别它们。
关于如何实现它的任何想法? (我已经阅读了所有的信号器文档......至少两次......)
最后,我意识到我的代码没有任何问题。 我只是通过更改本地 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">
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.