Authentication.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Authentication.js
  2. var usernameEntry;
  3. var passwordEntry;
  4. var username;
  5. var password;
  6. var textLoggedIn;
  7. var textNotLoggedIn;
  8. var buttonLogin;
  9. var buttonLogout;
  10. function pageLoad()
  11. {
  12. usernameEntry = $get("NameId");
  13. passwordEntry = $get("PwdId");
  14. username = $get("username");
  15. password = $get("password");
  16. textLoggedIn = $get("loggedin");
  17. textNotLoggedIn = $get("notloggedin");
  18. buttonLogin = $get("ButtonLogin");
  19. buttonLogout = $get("ButtonLogout");
  20. }
  21. // This function sets and gets the default
  22. // login completed callback function.
  23. function SetDefaultLoginCompletedCallBack()
  24. {
  25. // Set the default callback function.
  26. Sys.Services.AuthenticationService.set_defaultLoginCompletedCallback(OnLoginCompleted);
  27. // Get the default callback function.
  28. var callBack =
  29. Sys.Services.AuthenticationService.get_defaultLoginCompletedCallback();
  30. }
  31. // This function sets and gets the default
  32. // logout completed callback function.
  33. function SetDefaultLogoutCompletedCallBack()
  34. {
  35. // Set the default callback function.
  36. Sys.Services.AuthenticationService.set_defaultLogoutCompletedCallback(OnLogoutCompleted);
  37. // Get the default callback function.
  38. var callBack =
  39. Sys.Services.AuthenticationService.get_defaultLogoutCompletedCallback();
  40. }
  41. // This function sets and gets the default
  42. // failed callback function.
  43. function SetDefaultFailedCallBack()
  44. {
  45. // Set the default callback function.
  46. Sys.Services.AuthenticationService.set_defaultFailedCallback(OnFailed);
  47. // Get the default callback function.
  48. var callBack =
  49. Sys.Services.AuthenticationService.get_defaultFailedCallback();
  50. }
  51. // This function calls the login method of the
  52. // authentication service to verify
  53. // the credentials entered by the user.
  54. // If the credentials are authenticated, the
  55. // authentication service issues a forms
  56. // authentication cookie.
  57. function OnClickLogin()
  58. {
  59. // Set the default callback functions.
  60. SetDefaultLoginCompletedCallBack();
  61. SetDefaultLogoutCompletedCallBack();
  62. SetDefaultFailedCallBack();
  63. // Call the authetication service to authenticate
  64. // the credentials entered by the user.
  65. Sys.Services.AuthenticationService.login(username.value,
  66. password.value, false,null,null,null,null,"User Context");
  67. }
  68. // This function calls the logout method of the
  69. // authentication service to clear the forms
  70. // authentication cookie.
  71. function OnClickLogout()
  72. {
  73. // Clear the forms authentication cookie.
  74. Sys.Services.AuthenticationService.logout(null,
  75. null, null, null);
  76. }
  77. // This is the callback function called
  78. // if the authentication fails.
  79. function OnFailed(error,
  80. userContext, methodName)
  81. {
  82. // Display feedback message.
  83. DisplayInformation("error:message = " +
  84. error.get_message());
  85. DisplayInformation("error:timedOut = " +
  86. error.get_timedOut());
  87. DisplayInformation("error:statusCode = " +
  88. error.get_statusCode());
  89. }
  90. // The callback function called
  91. // if the authentication completed successfully.
  92. function OnLoginCompleted(validCredentials,
  93. userContext, methodName)
  94. {
  95. // Clear the user password.
  96. password.value = "";
  97. // On success there will be a forms
  98. // authentication cookie in the browser.
  99. if (validCredentials == true)
  100. {
  101. // Clear the user name.
  102. username.value = "";
  103. // Hide login fields.
  104. buttonLogin.style.visibility = "hidden";
  105. usernameEntry.style.visibility = "hidden";
  106. passwordEntry.style.visibility = "hidden";
  107. textNotLoggedIn.style.visibility = "hidden";
  108. // Display logout fields.
  109. buttonLogout.style.visibility = "visible";
  110. textLoggedIn.style.visibility = "visible";
  111. // Clear the feedback area.
  112. DisplayInformation("");
  113. }
  114. else
  115. {
  116. textLoggedIn.style.visibility = "hidden";
  117. textNotLoggedIn.style.visibility = "visible";
  118. DisplayInformation(
  119. "Login Credentials Invalid. Could not login");
  120. }
  121. }
  122. // This is the callback function called
  123. // if the user logged out successfully.
  124. function OnLogoutCompleted(result)
  125. {
  126. // Display login fields.
  127. usernameEntry.style.visibility = "visible";
  128. passwordEntry.style.visibility = "visible";
  129. textNotLoggedIn.style.visibility = "visible";
  130. buttonLogin.style.visibility = "visible";
  131. // Hide logout fields.
  132. buttonLogout.style.visibility = "hidden";
  133. textLoggedIn.style.visibility = "hidden";
  134. }
  135. // This function displays feedback
  136. // information for the user.
  137. function DisplayInformation(text)
  138. {
  139. document.getElementById("FeedBackID").innerHTML =
  140. "<br/>" + text;
  141. // Display authentication service information.
  142. var userLoggedIn =
  143. Sys.Services.AuthenticationService.get_isLoggedIn();
  144. var authServiceTimeout =
  145. Sys.Services.AuthenticationService.get_timeout();
  146. var userLoggedInfo =
  147. "<br/> User logged in: " + userLoggedIn;
  148. var timeOutInfo =
  149. "<br/> Authentication service timeout: " + authServiceTimeout;
  150. document.getElementById("FeedBackID").innerHTML =
  151. userLoggedInfo + timeOutInfo;
  152. }
  153. if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();