向使用 "Identity as UI" 的 Web 应用程序验证 .NET Core 2.1 SignalR 控制台客户端
Authenticate a .NET Core 2.1 SignalR console client to a web app that uses "Identity as UI"
使用 .NET Core 2.1 和 VS2017 预览版 2 我使用 "Identity as UI" 创建了一个简单的 Web 服务器,如 here and then added a SignalR chat following this 所述。
特别是我有:
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
也就是说,我不需要在登录时使用防伪令牌(无论如何都没有意义)
客户端代码是这样的:
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!");
(当然密码会在部署的其他地方)
使用 .NET Core 2.1 和 VS2017 预览版 2 我使用 "Identity as UI" 创建了一个简单的 Web 服务器,如 here and then added a SignalR chat following this 所述。
特别是我有:
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
也就是说,我不需要在登录时使用防伪令牌(无论如何都没有意义)
客户端代码是这样的:
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!");
(当然密码会在部署的其他地方)