SignalR 有 MVC IIS 客户端模拟当前 Windows 用户

SignalR have MVC IIS client Impersonate Current Windows User

我正在摆弄 SignalR,想找出可以在当前部署的 Intranet MVC 应用程序中使用它的好东西。当前设置如下所示(除了 signalr 之外已经存在):

架构:Windows 用户浏览器 --> 负载平衡 MVC 网络应用程序 --> SignalR 主机

有了这个架构,我想弄清楚我想用身份验证做的事情是否可行。我想要做的是让 MVC 应用程序(目前 运行 在 windows 服务帐户下)模拟当前登录到 MVC 应用程序而不是连接的 windows 用户到 SignalR 集线器本身。这样我就可以重用我已经内置到 MVC 应用程序中的身份验证,以防某些我不希望某些用户调用的东西,或者如果我想限制某些 SignalR 操作只允许 MVC 应用程序 运行 在服务帐户下执行。

我尝试使用 SignalR 文档中指出的以下示例,但它在服务器端不起作用(显然),因为默认凭据是服务帐户的凭据。

var hubConnection = new HubConnection(System.Configuration.ConfigurationManager.AppSettings["fwServiceAddress"].ToString());
            hubConnection.Credentials = CredentialCache.DefaultCredentials;
            IHubProxy customerHub = hubConnection.CreateHubProxy("customer");
            await hubConnection.Start();
            await customerHub.Invoke("NewNoteAdded", newNote);

当调用 SignalR 集线器时,由于应用程序的不同层,用户是服务帐户而不是实际的 windows 用户。如果没有办法模仿,我相信我可以发挥创意。

基于Connection.Credentials Property and CredentialCache.DefaultCredentials Property.

The DefaultCredentials property applies only to NTLM, negotiate, and Kerberos-based authentication. DefaultCredentials represents the system credentials for the current security context in which the application is running. For a client-side application, these are usually the Windows credentials (user name, password, and domain) of the user running the application. For ASP.NET applications, the default credentials are the user credentials of the logged-in user, or the user being impersonated.

假设您使用不同形式的身份验证,您可以根据当前登录用户创建您的凭据。看看ICredentials Interface.

代码有点长,请查看。实现 ICredentials 接口仅包含 returns NetworkCredential Class 一种方法。只需使用当前登录用户的凭据构造 NetworkCredentialClass 实例即可。

最后,你会得到这样的东西:

var hubConnection = new HubConnection(System.Configuration.ConfigurationManager.AppSettings["fwServiceAddress"].ToString());
hubConnection.Credentials = CredentialCache.DefaultCredentials; // returns ICredentials
IHubProxy customerHub = hubConnection.CreateHubProxy("customer");
await hubConnection.Start();
await customerHub.Invoke("NewNoteAdded", newNote);

请注意,登录用户是指登录您的 MVC 应用程序而不是客户端计算机。如果您想要活动目录用户的凭据,那么您还必须在您的 Web 应用程序上实施 windows 身份验证。参见 How to implement Windows authentication and authorization in ASP.NET