SampleCookieAuthentication.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #if !BESTHTTP_DISABLE_SIGNALR
  2. #if !BESTHTTP_DISABLE_COOKIES && (!UNITY_WEBGL || UNITY_EDITOR)
  3. using System;
  4. using BestHTTP.Cookies;
  5. using BestHTTP.SignalR.Transports;
  6. namespace BestHTTP.SignalR.Authentication
  7. {
  8. public sealed class SampleCookieAuthentication : IAuthenticationProvider
  9. {
  10. #region Public Properties
  11. public Uri AuthUri { get; private set; }
  12. public string UserName { get; private set; }
  13. public string Password { get; private set; }
  14. public string UserRoles { get; private set; }
  15. #endregion
  16. #region IAuthenticationProvider properties
  17. public bool IsPreAuthRequired { get; private set; }
  18. public event OnAuthenticationSuccededDelegate OnAuthenticationSucceded;
  19. public event OnAuthenticationFailedDelegate OnAuthenticationFailed;
  20. #endregion
  21. #region Privates
  22. private HTTPRequest AuthRequest;
  23. private Cookie Cookie;
  24. #endregion
  25. public SampleCookieAuthentication(Uri authUri, string user, string passwd, string roles)
  26. {
  27. this.AuthUri = authUri;
  28. this.UserName = user;
  29. this.Password = passwd;
  30. this.UserRoles = roles;
  31. this.IsPreAuthRequired = true;
  32. }
  33. #region IAuthenticationProvider Implementation
  34. public void StartAuthentication()
  35. {
  36. AuthRequest = new HTTPRequest(AuthUri, HTTPMethods.Post, OnAuthRequestFinished);
  37. // Setup the form
  38. AuthRequest.AddField("userName", UserName);
  39. AuthRequest.AddField("Password", Password); // not used in the sample
  40. AuthRequest.AddField("roles", UserRoles);
  41. AuthRequest.Send();
  42. }
  43. public void PrepareRequest(HTTPRequest request, RequestTypes type)
  44. {
  45. // Adding the cookie to the request is not required, as it's managed by the plugin automatically,
  46. // but for now, we want to be really sure that it's added
  47. request.Cookies.Add(Cookie);
  48. }
  49. #endregion
  50. #region Request Handler
  51. void OnAuthRequestFinished(HTTPRequest req, HTTPResponse resp)
  52. {
  53. AuthRequest = null;
  54. string failReason = string.Empty;
  55. switch (req.State)
  56. {
  57. // The request finished without any problem.
  58. case HTTPRequestStates.Finished:
  59. if (resp.IsSuccess)
  60. {
  61. Cookie = resp.Cookies != null ? resp.Cookies.Find(c => c.Name.Equals(".ASPXAUTH")) : null;
  62. if (Cookie != null)
  63. {
  64. HTTPManager.Logger.Information("CookieAuthentication", "Auth. Cookie found!");
  65. if (OnAuthenticationSucceded != null)
  66. OnAuthenticationSucceded(this);
  67. // return now, all other paths are authentication failures
  68. return;
  69. }
  70. else
  71. HTTPManager.Logger.Warning("CookieAuthentication", failReason = "Auth. Cookie NOT found!");
  72. }
  73. else
  74. HTTPManager.Logger.Warning("CookieAuthentication", failReason = string.Format("Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
  75. resp.StatusCode,
  76. resp.Message,
  77. resp.DataAsText));
  78. break;
  79. // The request finished with an unexpected error. The request's Exception property may contain more info about the error.
  80. case HTTPRequestStates.Error:
  81. HTTPManager.Logger.Warning("CookieAuthentication", failReason = "Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception"));
  82. break;
  83. // The request aborted, initiated by the user.
  84. case HTTPRequestStates.Aborted:
  85. HTTPManager.Logger.Warning("CookieAuthentication", failReason = "Request Aborted!");
  86. break;
  87. // Ceonnecting to the server is timed out.
  88. case HTTPRequestStates.ConnectionTimedOut:
  89. HTTPManager.Logger.Error("CookieAuthentication", failReason = "Connection Timed Out!");
  90. break;
  91. // The request didn't finished in the given time.
  92. case HTTPRequestStates.TimedOut:
  93. HTTPManager.Logger.Error("CookieAuthentication", failReason = "Processing the request Timed Out!");
  94. break;
  95. }
  96. if (OnAuthenticationFailed != null)
  97. OnAuthenticationFailed(this, failReason);
  98. }
  99. #endregion
  100. }
  101. }
  102. #endif
  103. #endif