从服务调用SignalR服务器端方法

时间:2014-04-11 10:13:10

标签: c# signalr

我的架构类似于下图。我想知道在我的服务(也是单独的应用程序 - SignalR server)上调用windows service(这是单独的IIS应用程序)上的集线器操作的热点。换句话说,我想通过services将通知从WebClients推送到SignalR server。在服务中使用.NET SignalR clients是唯一的方法吗?如果我想在[Authorize]方法上使用Hub属性,该怎么办?之后,我将不得不为我的服务创建单独的用户?

 ___________             ___________              ___________
|           |           |           |            |           |
|           |---------->|           |            |           |
| WebClient |           |  SignalR  |<-----------| Service1  |
|           |<----------|           |            |           |
|___________|           |___________|            |___________|
                             .
                            /|\
                             |
                             |
                         ___________             
                        |           |
                        |           |
                        | Service2  |           
                        |           |           
                        |___________|           

1 个答案:

答案 0 :(得分:0)

是的,您可以在Windows服务中使用.NET客户端库。它们的行为方式与任何其他SignalR客户端相同。

在asp.net/signalr上有很多关于SignalR的综合例子。

Authentication options for .NET clients

进行身份验证的一种方法是让一个单独的页面只设置身份验证cookie,然后从Response中选择cookie,然后在执行调用时在运行的Windows服务实例中使用它使用Hub装饰的[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;
        }
    }

https://www.contoso.com/RemoteLogin的代码段(在上例中使用):

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);
            }
        }
    }
}