使用.NET Core 2.1& VS2017预览2我按照此处,然后在此.
Using .NET Core 2.1 & VS2017 preview 2 I created a simple web server with "Identity as UI" as explained 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": "localhost:5000", "sslPort": 0我启动带浏览器的调试器,注册用户并登录,然后转到 localhost: 5000/SignalRtest (我的剃须刀页面使用signalr.js),并确认聊天是否正常.
I start the debugger which brings the browser, register a user and log in, then go to localhost:5000/SignalRtest (my razor page that uses signalr.js) and verify the chat works fine.
我现在尝试创建.NET Core控制台应用聊天客户端:
I now try to create a .NET Core console app chat client:
class Program { public static async Task SetupSignalRHubAsync() { var hubConnection = new HubConnectionBuilder() .WithUrl("localhost:5000/hubs/myhub") .Build(); await hubConnection.StartAsync(); await hubConnection.SendAsync("Send", "consoleapp"); } public static void Main(string[] args) { SetupSignalRHubAsync().Wait(); } }我的问题是我不知道如何验证此客户端?
My issue is I don't know how to authenticate this client ?
(来自 github/aspnet/SignalR/issues/2455)
它在浏览器中起作用的原因是,浏览器具有 登录时的Cookie,因此它会在发送时自动发送 SignalR发出请求.在.NET中做类似的事情 客户端,您必须在服务器上调用REST API,该API可以设置 cookie,从HttpClient中挖出cookie,然后在 调用.WithUrl,如下所示:
"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("localhost:5000/hubs/myhub", options => { options.Cookies.Add(loginCookie); }) .Build();
我现在悬赏这个问题,希望能找到一个解决方案,该方案显示如何使用使用身份作为UI"的.NET Core 2.1 Web应用程序SignalR服务器对.NET Core 2.1 SignalR控制台客户端进行身份验证.我需要从服务器获取cookie,然后将其添加到SignalR HubConnectionBuilder(现在具有WithCookie方法).
I now put a bounty on this question, hoping to get a solution showing how to authenticate the .NET Core 2.1 SignalR console client with a .NET Core 2.1 web app SignalR server that uses "Identity as UI". I need to get the cookie from the server and then add it to SignalR HubConnectionBuilder (which now have a WithCookie method).
请注意,我并不是在寻找IdentityServer之类的第三方解决方案 谢谢!
Note that I am not looking for a third-party solutions like IdentityServer Thanks!
推荐答案我确实最终使它像这样工作:
I did ended up getting this to work like this:
在服务器上,我搭建了登录名,然后在Login.cshtml.cs中添加了
On the server I scaffolded Login and then in Login.cshtml.cs added
[AllowAnonymous] [IgnoreAntiforgeryToken(Order = 1001)] public class LoginModel : PageModel那是我在登录时不需要防伪令牌(无论如何也没有意义)
That is I do not require the anti forgery token on login (which doesn't make sense anyway)
客户端代码如下:
HttpClientHandler handler = new HttpClientHandler(); CookieContainer cookies = new CookieContainer(); handler.CookieContainer = cookies; HttpClient client = new HttpClient(handler); var uri = new Uri("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("localhost:5000/hubs/myhub", options => { options.Cookies.Add(authCookie); }) .Build(); await hubConnection.StartAsync(); await hubConnection.SendAsync("Send", "hello!");(密码当然会在部署中的其他位置)
(of course password will be elsewhere in deployment)
更多推荐
向使用“身份作为UI"的Web应用程序对.NET Core 2.1 SignalR控制台客户端进行身份验证.
发布评论