asp.net-core - 向使用 "Identity as UI"的 Web 应用验证 .NET Core 2.1 SignalR 控制台客户端

标签 asp.net-core signalr signalr-hub signalr.client asp.net-core-2.1

使用 .NET Core 2.1 和 VS2017 预览版 2 我创建了一个简单的 Web 服务器,其中包含“Identity as UI”,解释为 here然后在 this 后面添加了 SignalR 聊天.

特别是我有:

app.UseAuthentication();
app.UseSignalR((options) => {
    options.MapHub<MyHub>("/hubs/myhub");
});

..

[Authorize]
public class MyHub : Hub

..

"iisSettings": {
  "windowsAuthentication": false,
  "anonymousAuthentication": true,
     "iisExpress": {
     "applicationUrl": "http://localhost:5000",
     "sslPort": 0

我启动带有浏览器的调试器,注册用户并登录,然后转到 http://localhost:5000/SignalRtest (我使用 signalr.js 的 razor 页面)并验证聊天工作正常。

我现在尝试创建一个 .NET Core 控制台应用聊天客户端:
class Program
{
    public static async Task SetupSignalRHubAsync()
    {
        var hubConnection = new HubConnectionBuilder()
                    .WithUrl("http://localhost:5000/hubs/myhub")
                    .Build();

        await hubConnection.StartAsync();
        await hubConnection.SendAsync("Send", "consoleapp");
    }

    public static void Main(string[] args)
    {
        SetupSignalRHubAsync().Wait();
    }
}

我的问题是我不知道如何验证这个客户端?

编辑:

(来自 https://github.com/aspnet/SignalR/issues/2455)

"The reason it works in the browser is because the browser has a Cookie from when you logged in, so it sends it automatically when SignalR makes it's requests. To do something similar in the .NET client you'll have to call a REST API on your server that can set the cookie, scoop the cookie up from HttpClient and then provide it in the call to .WithUrl, like so:

var loginCookie = /* get the cookie */
var hubConnection = new HubConnectionBuilder()
    .WithUrl("http://localhost:5000/hubs/myhub", options => {
        options.Cookies.Add(loginCookie);
    })
    .Build();


我现在悬赏这个问题,希望能得到一个解决方案,展示如何使用使用“Identity as UI”的 .NET Core 2.1 Web 应用 SignalR 服务器对 .NET Core 2.1 SignalR 控制台客户端进行身份验证。我需要从服务器获取 cookie,然后将其添加到 SignalR HubConnectionBuilder(现在有一个 WithCookie 方法)。

请注意,我不是在寻找像 IdentityServer 这样的第三方解决方案
谢谢!

最佳答案

我确实最终让它像这样工作:

在服务器上,我搭建了登录脚手架,然后在 Login.cshtml.cs 添加

[AllowAnonymous]
[IgnoreAntiforgeryToken(Order = 1001)]
public class LoginModel : PageModel

那就是我在登录时不需要防伪 token (无论如何这没有意义)

客户端代码是这样的:
HttpClientHandler handler = new HttpClientHandler();
CookieContainer cookies = new CookieContainer();
handler.CookieContainer = cookies;
HttpClient client = new HttpClient(handler);

var uri = new Uri("http://localhost:5000/Identity/Account/Login");
string jsonInString = "Input.Email=myemail&Input.Password=mypassword&Input.RememberMe=false";
HttpResponseMessage response = await client.PostAsync(uri, new StringContent(jsonInString, System.Text.Encoding.UTF8, "application/x-www-form-urlencoded"));
var responseCookies = cookies.GetCookies(uri);
var authCookie = responseCookies[".AspNetCore.Identity.Application"];

var hubConnection = new HubConnectionBuilder()
        .WithUrl("http://localhost:5000/hubs/myhub", options =>
        {
            options.Cookies.Add(authCookie);
        })
        .Build();

await hubConnection.StartAsync();
await hubConnection.SendAsync("Send", "hello!");

(当然密码将在部署的其他地方)

关于asp.net-core - 向使用 "Identity as UI"的 Web 应用验证 .NET Core 2.1 SignalR 控制台客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50752724/

相关文章:

c# - ASP.NET Core 2 - 全局过滤器注册多种方式?

signalr - 向组中除当前客户端之外的所有客户端发送消息

signalr - 启动时调用 SignalR 客户端方法

c# - 这两种 Post 方法有什么区别

c# - 将 ASP.Net Core Angular 模板从版本 8 更新到 9

SignalR "Error during negotiation request"

asp.net - 在 Amazon AWS 上扩展 SignalR

SignalR:在同一网站上传/下载文件之前没有消息传递

asp.net - 由于 HttpPlatformHandler,Aurelia 无法在 azure 上加载?

asp.net - MapHubs 不映射中心并在 X 应用程序中返回错误