SampleHeaderAuthentication.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #if !BESTHTTP_DISABLE_SIGNALR
  2. namespace BestHTTP.SignalR.Authentication
  3. {
  4. /// <summary>
  5. /// Custom http-header based authenticator.
  6. /// <example>
  7. /// <code>
  8. /// // Server side implementation of the Header-based authenticator
  9. /// // Use it by adding the app.Use(typeof(HeaderBasedAuthenticationMiddleware)); line to the Startup class' Configuration function.
  10. /// private class HeaderBasedAuthenticationMiddleware : OwinMiddleware
  11. /// {
  12. /// public HeaderBasedAuthenticationMiddleware(OwinMiddleware next)
  13. /// : base(next)
  14. /// {
  15. /// }
  16. ///
  17. /// public override Task Invoke(IOwinContext context)
  18. /// {
  19. /// string username = context.Request.Headers.Get("username");
  20. /// string roles = context.Request.Headers.Get("roles");
  21. ///
  22. /// if (!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(roles))
  23. /// {
  24. /// var identity = new System.Security.Principal.GenericIdentity(username);
  25. ///
  26. /// var principal = new System.Security.Principal.GenericPrincipal(identity, SplitString(roles));
  27. ///
  28. /// context.Request.User = principal;
  29. /// }
  30. ///
  31. /// return Next.Invoke(context);
  32. /// }
  33. ///
  34. /// private static string[] SplitString(string original)
  35. /// {
  36. /// if (String.IsNullOrEmpty(original))
  37. /// return new string[0];
  38. ///
  39. /// var split = from piece in original.Split(',') let trimmed = piece.Trim() where !String.IsNullOrEmpty(trimmed) select trimmed;
  40. ///
  41. /// return split.ToArray();
  42. /// }
  43. /// }
  44. /// </code>
  45. /// </example>
  46. /// </summary>
  47. class HeaderAuthenticator : IAuthenticationProvider
  48. {
  49. public string User { get; private set; }
  50. public string Roles { get; private set; }
  51. /// <summary>
  52. /// No pre-auth step required for this type of authentication
  53. /// </summary>
  54. public bool IsPreAuthRequired { get { return false; } }
  55. #pragma warning disable 0067
  56. /// <summary>
  57. /// Not used event as IsPreAuthRequired is false
  58. /// </summary>
  59. public event OnAuthenticationSuccededDelegate OnAuthenticationSucceded;
  60. /// <summary>
  61. /// Not used event as IsPreAuthRequired is false
  62. /// </summary>
  63. public event OnAuthenticationFailedDelegate OnAuthenticationFailed;
  64. #pragma warning restore 0067
  65. /// <summary>
  66. /// Constructor to initialise the authenticator with username and roles.
  67. /// </summary>
  68. public HeaderAuthenticator(string user, string roles)
  69. {
  70. this.User = user;
  71. this.Roles = roles;
  72. }
  73. /// <summary>
  74. /// Not used as IsPreAuthRequired is false
  75. /// </summary>
  76. public void StartAuthentication()
  77. { }
  78. /// <summary>
  79. /// Prepares the request by adding two headers to it
  80. /// </summary>
  81. public void PrepareRequest(BestHTTP.HTTPRequest request, RequestTypes type)
  82. {
  83. request.SetHeader("username", this.User);
  84. request.SetHeader("roles", this.Roles);
  85. }
  86. }
  87. }
  88. #endif