C# 从服务调用SignalR服务器端方法
我的架构与下图类似。我想知道从我的服务(也是独立的应用程序-C# 从服务调用SignalR服务器端方法,c#,signalr,C#,Signalr,我的架构与下图类似。我想知道从我的服务(也是独立的应用程序-windows服务)调用signar服务器上的集线器操作(这是独立的IIS应用程序)有多热。换句话说,我想通过信号服务器将通知从我的服务推送到网络客户端。在服务中使用.NET signar clients是唯一的方法吗?如果我想在Hub方法上使用[Authorize]属性,该怎么办?之后,我将不得不为我的服务创建单独的用户 ___________ ___________ _______
windows服务
)调用signar服务器上的集线器操作(这是独立的IIS应用程序)有多热。换句话说,我想通过信号服务器将通知从我的服务
推送到网络客户端
。在服务中使用.NET signar clients
是唯一的方法吗?如果我想在Hub
方法上使用[Authorize]
属性,该怎么办?之后,我将不得不为我的服务创建单独的用户
___________ ___________ ___________
| | | | | |
| |---------->| | | |
| WebClient | | SignalR |<-----------| Service1 |
| |<----------| | | |
|___________| |___________| |___________|
.
/|\
|
|
___________
| |
| |
| Service2 |
| |
|___________|
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu___________
| | | | | |
| |---------->| | | |
|WebClient | | signal |是的,您可以在windows服务中使用.NET客户端库。它们的行为方式将与任何其他信号器客户端相同
在asp.net/SignalR上有许多关于SignalR的综合示例
进行身份验证的一种方法是使用一个单独的页面来设置身份验证cookie,然后从响应中选取cookie,然后在运行的windows服务实例中使用cookie在中心上执行方法调用,该中心用[Authorize]
修饰
文档中的代码段:
static void Main(string[] args)
{
var connection = new HubConnection("http://www.contoso.com/");
Cookie returnedCookie;
Console.Write("Enter user name: ");
string username = Console.ReadLine();
Console.Write("Enter password: ");
string password = Console.ReadLine();
var authResult = AuthenticateUser(username, password, out returnedCookie);
if (authResult)
{
connection.CookieContainer = new CookieContainer();
connection.CookieContainer.Add(returnedCookie);
Console.WriteLine("Welcome " + username);
}
else
{
Console.WriteLine("Login failed");
}
}
private static bool AuthenticateUser(string user, string password, out Cookie authCookie)
{
var request = WebRequest.Create("https://www.contoso.com/RemoteLogin") as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = new CookieContainer();
var authCredentials = "UserName=" + user + "&Password=" + password;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(authCredentials);
request.ContentLength = bytes.Length;
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
using (var response = request.GetResponse() as HttpWebResponse)
{
authCookie = response.Cookies[FormsAuthentication.FormsCookieName];
}
if (authCookie != null)
{
return true;
}
else
{
return false;
}
}
的代码段(在上面的示例中使用):
您可以使用.Net客户端连接并调用集线器上的方法。服务1和服务2是什么类型?它们是windows服务。
namespace SignalRWithConsoleChat
{
public partial class RemoteLogin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string username = Request["UserName"];
string password = Request["Password"];
bool result = Membership.ValidateUser(username, password);
if (result)
{
FormsAuthentication.SetAuthCookie(username, false);
}
}
}
}