{"id":1204,"date":"2013-10-11T16:28:09","date_gmt":"2013-10-11T16:28:09","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/webdev\/2013\/10\/11\/signalr-and-user-identity-authentication-and-authorization\/"},"modified":"2013-10-11T16:28:09","modified_gmt":"2013-10-11T16:28:09","slug":"signalr-and-user-identity-authentication-and-authorization","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/signalr-and-user-identity-authentication-and-authorization\/","title":{"rendered":"SignalR and user identity (authentication and authorization)"},"content":{"rendered":"<p>There are too many authentication types (Basic, Windows, Cookie, OAuth) to explain how to use all of them. In this sample, I focus on using Cookie Authentication to secure a website, a Persistent Connection, and a Hub. Authentication is configured on OWIN, you have to add some nuget packages and add code in Startup.cs. I started with a web project using the MVC Template. By default, it creates web forms to register users, input user credentials, configures an anti-forgery token for http requests, creates an entity framework repository for User Identity. For the self host server there is no template, so I created it using the previous project as sample but removed things like the anti-forgery token, MVC, and entity framework.<\/p>\n<p>The important thing to remember is OWIN takes care of authentication and all frameworks on top of OWIN (SignalR, MVC, WebApi, etc) simply consume the user identity provided by OWIN. So, if you can\u2019t see the identity on SignalR, the problem is in your OWIN configuration.<\/p>\n<p>A <strong>SignalR Persistent Connection<\/strong> gives you access to the user identity by overriding AuthorizeRequest method. The sample code below allows to create a persistent connection only to authenticated users. you could add more logic to allow only some user roles by using the method request.User.IsInRole(string role)&#160;&#160; <\/p>\n<table cellspacing=\"0\" cellpadding=\"2\" width=\"700\" border=\"1\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"698\">\n<pre class=\"code\"><span style=\"background: white;color: blue\">namespace <\/span><span style=\"background: white;color: black\">Common.Connections\n{\n  <\/span><span style=\"background: white;color: blue\">public class <\/span><span>AuthorizeEchoConnection <\/span><span style=\"background: white;color: black\">: <\/span><span>PersistentConnection\n  <\/span><span style=\"background: white;color: black\">{\n    <\/span><span style=\"background: white;color: blue\">protected override bool <\/span><span style=\"background: white;color: black\">AuthorizeRequest(<\/span><span>IRequest <\/span><span style=\"background: white;color: black\">request)\n    {\n      <\/span><span style=\"background: white;color: blue\">return <\/span><span style=\"background: white;color: black\">request.User != <\/span><span style=\"background: white;color: blue\">null <\/span><span style=\"background: white;color: black\">&amp;&amp; request.User.Identity.IsAuthenticated;\n    }\n\n    ...\n\n  }\n}<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><\/p>\n<p>A <strong>SignalR Hub<\/strong> gives you access to the user identity using Context.User. If you want to restrict access to a Hub only to authenticated users, add the [Authorize] attribute. Do you want to allow only some user roles? Add [Authorize(Roles=&quot;myRole&quot;)]. Do you want to allow specific users? Add [Authorize(Users=&quot;myUser&quot;)]<\/p>\n<table cellspacing=\"0\" cellpadding=\"2\" width=\"700\" border=\"1\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"698\">\n<pre class=\"code\"><span style=\"background: white;color: blue\">namespace <\/span><span style=\"background: white;color: black\">Common.Hubs\n{\n  [<\/span><span>Authorize<\/span><span style=\"background: white;color: black\">]\n  <\/span><span style=\"background: white;color: blue\">public class <\/span><span>AuthorizeEchoHub <\/span><span style=\"background: white;color: black\">: <\/span><span>Hub\n  <\/span><span style=\"background: white;color: black\">{\n    <\/span><span style=\"background: white;color: blue\">public override <\/span><span>Task <\/span><span style=\"background: white;color: black\">OnConnected()\n    {\n      <\/span><span style=\"background: white;color: blue\">return <\/span><span style=\"background: white;color: black\">Clients.Caller.hubReceived(<\/span><span>&quot;Welcome &quot; <\/span><span style=\"background: white;color: black\">+ Context.User.Identity.Name + <\/span><span>&quot;!&quot;<\/span><span style=\"background: white;color: black\">);\n    }\n\n    ...\n\n  }\n}<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><\/p>\n<p>Full sample code is <a href=\"https:\/\/github.com\/gustavo-armenta\/CookieAuthenticationSample\">here<\/a>. It contains a web host server and a self host server. Then you can use any of the clients to authenticate and establish a SignalR connection:<\/p>\n<ul>\n<li>JavaScript client connecting as cross-domain<\/li>\n<li>C# console client<\/li>\n<li>C# windows phone<\/li>\n<li>C# windows store app<\/li>\n<\/ul>\n<p>For more information, <a href=\"http:\/\/www.asp.net\/signalr\/overview\/security\">read SignalR documentation about security<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are too many authentication types (Basic, Windows, Cookie, OAuth) to explain how to use all of them. In this sample, I focus on using Cookie Authentication to secure a website, a Persistent Connection, and a Hub. Authentication is configured on OWIN, you have to add some nuget packages and add code in Startup.cs. I [&hellip;]<\/p>\n","protected":false},"author":402,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[7408],"class_list":["post-1204","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet","tag-signalr"],"acf":[],"blog_post_summary":"<p>There are too many authentication types (Basic, Windows, Cookie, OAuth) to explain how to use all of them. In this sample, I focus on using Cookie Authentication to secure a website, a Persistent Connection, and a Hub. Authentication is configured on OWIN, you have to add some nuget packages and add code in Startup.cs. I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/1204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/402"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=1204"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/1204\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=1204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=1204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=1204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}