LoginControl.ascx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <%@ Control Language="C#" ClassName="LoginControl" %>
  2. <%@Import Namespace="System.Web.UI" %>
  3. <script language="C#" runat="server">
  4. protected void Page_Load(object sender, EventArgs e)
  5. {
  6. ScriptManager sm = ScriptManager.GetCurrent(this.Page);
  7. if (sm == null)
  8. {
  9. throw new InvalidOperationException(
  10. "Script Manager not defined. Create one in the page where you use this control.");
  11. }
  12. }
  13. </script>
  14. <script type="text/javascript">
  15. // Authentication.js
  16. var usernameEntry;
  17. var passwordEntry;
  18. var username;
  19. var password;
  20. var textLoggedIn;
  21. var textNotLoggedIn;
  22. var buttonLogin;
  23. var buttonLogout;
  24. function pageLoad()
  25. {
  26. usernameEntry = $get("NameId");
  27. passwordEntry = $get("PwdId");
  28. username = $get("username");
  29. password = $get("password");
  30. textLoggedIn = $get("loggedin");
  31. textNotLoggedIn = $get("notloggedin");
  32. buttonLogin = $get("ButtonLogin");
  33. buttonLogout = $get("ButtonLogout");
  34. }
  35. // This function sets and gets the default
  36. // login completed callback function.
  37. function SetDefaultLoginCompletedCallBack()
  38. {
  39. // Set the default callback function.
  40. Sys.Services.AuthenticationService.set_defaultLoginCompletedCallback(OnLoginCompleted);
  41. // Get the default callback function.
  42. var callBack =
  43. Sys.Services.AuthenticationService.get_defaultLoginCompletedCallback();
  44. }
  45. // This function sets and gets the default
  46. // logout completed callback function.
  47. function SetDefaultLogoutCompletedCallBack()
  48. {
  49. // Set the default callback function.
  50. Sys.Services.AuthenticationService.set_defaultLogoutCompletedCallback(OnLogoutCompleted);
  51. // Get the default callback function.
  52. var callBack =
  53. Sys.Services.AuthenticationService.get_defaultLogoutCompletedCallback();
  54. }
  55. // This function sets and gets the default
  56. // failed callback function.
  57. function SetDefaultFailedCallBack()
  58. {
  59. // Set the default callback function.
  60. Sys.Services.AuthenticationService.set_defaultFailedCallback(OnFailed);
  61. // Get the default callback function.
  62. var callBack =
  63. Sys.Services.AuthenticationService.get_defaultFailedCallback();
  64. }
  65. // This function calls the login method of the
  66. // authentication service to verify
  67. // the credentials entered by the user.
  68. // If the credentials are authenticated, the
  69. // authentication service issues a forms
  70. // authentication cookie.
  71. function OnClickLogin()
  72. {
  73. // Set the default callback functions.
  74. SetDefaultLoginCompletedCallBack();
  75. SetDefaultLogoutCompletedCallBack();
  76. SetDefaultFailedCallBack();
  77. // Call the authetication service to authenticate
  78. // the credentials entered by the user.
  79. Sys.Services.AuthenticationService.login(username.value,
  80. password.value, false,null,null,null,null,"User Context");
  81. }
  82. // This function calls the logout method of the
  83. // authentication service to clear the forms
  84. // authentication cookie.
  85. function OnClickLogout()
  86. {
  87. // Clear the forms authentication cookie.
  88. Sys.Services.AuthenticationService.logout(null,
  89. null, null, null);
  90. }
  91. // This is the callback function called
  92. // if the authentication fails.
  93. function OnFailed(error,
  94. userContext, methodName)
  95. {
  96. // Display feedback message.
  97. DisplayInformation("error:message = " +
  98. error.get_message());
  99. DisplayInformation("error:timedOut = " +
  100. error.get_timedOut());
  101. DisplayInformation("error:statusCode = " +
  102. error.get_statusCode());
  103. }
  104. // The callback function called
  105. // if the authentication completed successfully.
  106. function OnLoginCompleted(validCredentials,
  107. userContext, methodName)
  108. {
  109. // Clear the user password.
  110. password.value = "";
  111. // On success there will be a forms
  112. // authentication cookie in the browser.
  113. if (validCredentials == true)
  114. {
  115. // Clear the user name.
  116. username.value = "";
  117. // Hide login fields.
  118. buttonLogin.style.visibility = "hidden";
  119. usernameEntry.style.visibility = "hidden";
  120. passwordEntry.style.visibility = "hidden";
  121. textNotLoggedIn.style.visibility = "hidden";
  122. // Display logout fields.
  123. buttonLogout.style.visibility = "visible";
  124. textLoggedIn.style.visibility = "visible";
  125. // Clear the feedback area.
  126. DisplayInformation("");
  127. }
  128. else
  129. {
  130. textLoggedIn.style.visibility = "hidden";
  131. textNotLoggedIn.style.visibility = "visible";
  132. DisplayInformation(
  133. "Login Credentials Invalid. Could not login");
  134. }
  135. }
  136. // This is the callback function called
  137. // if the user logged out successfully.
  138. function OnLogoutCompleted(result)
  139. {
  140. // Display login fields.
  141. usernameEntry.style.visibility = "visible";
  142. passwordEntry.style.visibility = "visible";
  143. textNotLoggedIn.style.visibility = "visible";
  144. buttonLogin.style.visibility = "visible";
  145. // Hide logout fields.
  146. buttonLogout.style.visibility = "hidden";
  147. textLoggedIn.style.visibility = "hidden";
  148. }
  149. // This function displays feedback
  150. // information for the user.
  151. function DisplayInformation(text)
  152. {
  153. document.getElementById("FeedBackID").innerHTML =
  154. "<br/>" + text;
  155. // Display authentication service information.
  156. var userLoggedIn =
  157. Sys.Services.AuthenticationService.get_isLoggedIn();
  158. var authServiceTimeout =
  159. Sys.Services.AuthenticationService.get_timeout();
  160. var userLoggedInfo =
  161. "<br/> User logged in: " + userLoggedIn;
  162. var timeOutInfo =
  163. "<br/> Authentication service timeout: " + authServiceTimeout;
  164. document.getElementById("FeedBackID").innerHTML =
  165. userLoggedInfo + timeOutInfo;
  166. }
  167. </script>
  168. <center>
  169. <span id="loggedin"
  170. style="visibility:hidden; color:Green; font-weight:bold; font-size:large"
  171. visible="false"><b>You are logged in! </b>
  172. </span>
  173. <span id="notloggedin"
  174. style="visibility:visible;color:Red; font-weight:bold; font-size:large">
  175. You are logged out!
  176. </span>
  177. <table>
  178. <tr id="NameId" style="visibility:visible;">
  179. <td style="background-color:Yellow; font-weight:bold; color:Red">
  180. User Name:
  181. </td>
  182. <td>
  183. <input type="text" id="username"/>
  184. </td>
  185. </tr>
  186. <tr id="PwdId" style="visibility:visible;">
  187. <td style="background-color:Yellow; font-weight:bold; color:Red">
  188. Password:
  189. </td>
  190. <td>
  191. <input type="password" id="password" />
  192. </td>
  193. </tr>
  194. <tr>
  195. <td colspan="2" align="center">
  196. <button id="ButtonLogin"
  197. onclick="OnClickLogin(); return false;">Login</button>
  198. <button id="ButtonLogout"
  199. style="visibility:hidden;"
  200. onclick="OnClickLogout(); return false;">Logout</button>
  201. </td>
  202. </tr>
  203. </table>
  204. </center>
  205. <hr />
  206. <div id="FeedBackID" style="visibility:visible" />