c# - Invoke SignalR server side methods from services -
i have architecture similar @ diagram below. wonder hot invoke hub operations on signalr server
(which separate iis application) services (also separate applications - windows service
). in other words want push notification services
webclients
through signalr server
. using .net signalr clients
in services way ? if want use [authorize]
attribute on hub
methods ? after have create separate users services ?
___________ ___________ ___________ | | | | | | | |---------->| | | | | webclient | | signalr |<-----------| service1 | | |<----------| | | | |___________| |___________| |___________| . /|\ | | ___________ | | | | | service2 | | | |___________|
yes, can use .net client library within windows services. behave in same manner other signalr client.
there many comprehensive examples signalr on @ asp.net/signalr.
authentication options .net clients
one way authentication have separate page set authentication cookie, , pick cookie response
, , use within running windows service instance when performing calls methods on hub
decorated [authorize]
.
code snippet documentation:
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") 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() httpwebresponse) { authcookie = response.cookies[formsauthentication.formscookiename]; } if (authcookie != null) { return true; } else { return false; } }
snippet https://www.contoso.com/remotelogin (used in above example):
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); } } } }
Comments
Post a Comment