Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.42 KB | None | 0 0
  1. @using Project.Models
  2. @model LoginInfo
  3.  
  4. <head>
  5. <meta charset="utf-8" />
  6. <meta name="viewport" content="width=device-width" />
  7. <title>Login</title>
  8. <script src="~/Scripts/jquery-2.2.0.js"></script>
  9. <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
  10. <script src="/Scripts/jquery.validate.min.js"></script>
  11. <script src="/Scripts/jquery.validate.unobtrusive.min.js"></script>
  12. <script src="~/Content/Scripts/login.js"></script>
  13. <script>
  14. $('html').removeClass('nojs');
  15. </script>
  16. </head>
  17. <body>
  18. <div id="container">
  19. @using (Ajax.BeginForm("Ajax", "Login", new AjaxOptions { OnBegin = "submit", OnComplete = "response" }))
  20. {
  21. <img id="logo" src="~/Content/Images/Main.png" alt="" />
  22. <table>
  23. <tr>
  24. <td>
  25. @Html.LabelFor(x => x.serverHost)
  26. </td>
  27. <td>
  28. @Html.TextBoxFor(x => x.serverHost, new { placeholder = "domain", required = "required" })
  29. </td>
  30. </tr>
  31. <tr>
  32. <td>
  33. @Html.LabelFor(x => x.instanceName)
  34. </td>
  35. <td>
  36. @Html.TextBoxFor(x => x.instanceName, new { placeholder = "company", required = "required" })
  37. </td>
  38. </tr>
  39. <tr>
  40. <td>
  41. @Html.LabelFor(x => x.username)
  42. </td>
  43. <td>
  44. @Html.TextBoxFor(x => x.username, new { placeholder = "Username", required = "required" })
  45. </td>
  46. </tr>
  47. <tr>
  48. <td>
  49. @Html.LabelFor(x => x.password)
  50. </td>
  51. <td>
  52. @Html.PasswordFor(x => x.password, new { placeholder = "Password", required = "required" })
  53. </td>
  54. </tr>
  55. </table>
  56. <table>
  57. <tr>
  58. <td id="remembertd">
  59. @Html.LabelFor(x => x.remember, "Remember me")
  60. @Html.CheckBoxFor(x => x.remember, new { @checked = Model.remember })
  61. </td>
  62. <td>
  63. <input id="submit" type="submit" value="Connect" />
  64. </td>
  65. </tr>
  66. </table>
  67. }
  68. </div>
  69. </body>
  70. </html>
  71.  
  72. [AllowAnonymous]
  73. public class LoginController : Controller
  74. {
  75. public ActionResult Index()
  76. {
  77. return View(new LoginInfo());
  78. }
  79.  
  80. [ValidateInput(true)]
  81. public ActionResult Ajax(LoginInfo info)
  82. {
  83. HttpStatusCodeResult result = new HttpStatusCodeResult(200);
  84.  
  85. if (!ModelState.IsValid)
  86. {
  87. result = GetFailedResult(new ModelStateInvalidException(), Reason.ModelStateInvalid);
  88. }
  89. else
  90. {
  91. try
  92. {
  93. var i = 0;
  94.  
  95. var j = 10 / i;
  96.  
  97. }
  98. catch (Exception ex)
  99. {
  100. result = GetFailedResult(ex, Reason.Unhandled);
  101. }
  102. }
  103.  
  104. return new HttpStatusCodeResult(result.StatusCode, result.StatusDescription);
  105. }
  106.  
  107. private HttpStatusCodeResult GetFailedResult(Exception ex, Reason reason)
  108. {
  109. HttpStatusCode httpErrorCode = HttpStatusCode.InternalServerError;
  110. String errorMessage = String.Empty;
  111.  
  112. //Trace
  113. RemotingHelper.Trace("Login attempt failed with message: " + ex.Message + "n----------");
  114.  
  115. //Get message/code
  116. switch (reason)
  117. {
  118. case Reason.IncorrectInstanceName:
  119. case Reason.IncorrectServerHost:
  120. case Reason.IncorrectServerHostPort:
  121. case Reason.IncorrectUsernameOrPassword:
  122. httpErrorCode = HttpStatusCode.Forbidden;
  123. errorMessage = "Could not connect using the provided details.";
  124. break;
  125.  
  126. case Reason.Offline:
  127. httpErrorCode = HttpStatusCode.ServiceUnavailable;
  128. errorMessage = "Could not establish connection with the server.";
  129. break;
  130.  
  131. case Reason.LicenseMissing:
  132. httpErrorCode = HttpStatusCode.InternalServerError;
  133. errorMessage = "There was a problem with your license.nPlease contact an administrator.";
  134. break;
  135.  
  136. default:
  137. httpErrorCode = HttpStatusCode.InternalServerError;
  138. errorMessage = "There was an error attempting to log in.nPlease contact an administrator.";
  139. break;
  140. }
  141.  
  142. //Return
  143. return new HttpStatusCodeResult(httpErrorCode, errorMessage + ":" + ((Int32)reason).ToString());
  144. }
  145. }
  146.  
  147. public enum Reason
  148. {
  149. Network = 10,
  150. Offline = 11,
  151. LicenseMissing = 12,
  152. LicenseFileMissing,
  153. NullChannel = 14,
  154. ModelStateInvalid = 15,
  155. Unhandled = 16,
  156. IncorrectServerHost = 17,
  157. IncorrectServerHostPort = 18,
  158. IncorrectInstanceName = 19,
  159. IncorrectUsernameOrPassword = 20
  160. }
  161.  
  162. public class LoginInfo
  163. {
  164. public LoginInfo()
  165. {
  166. serverHost = string.Empty;
  167. instanceName = string.Empty;
  168. username = string.Empty;
  169. password = string.Empty;
  170. remotingPassword = null;
  171. remember = false;
  172. }
  173.  
  174. [Required]
  175. [Display(Name = "Server Host")]
  176. public string serverHost { get; set; }
  177.  
  178. [Required]
  179. [Display(Name = "Instance Name")]
  180. public string instanceName { get; set; }
  181.  
  182. [Required]
  183. [Display(Name = "Username")]
  184. public string username { get; set; }
  185.  
  186. [Required]
  187. [Display(Name = "Password")]
  188. public string password { get; set; }
  189.  
  190. [Display(Name = "Remoting Password")]
  191. public string remotingPassword { get; set; }
  192.  
  193. [Display(Name = "Persistent")]
  194. public bool remember { get; set; }
  195. }
  196.  
  197. function response(data) {
  198. setTimeout(function () {
  199. if (data.status == 200) {
  200. success();
  201. }
  202. else {
  203. var info = data.statusText.split('|');
  204. var message = info[0];
  205. var code = info[1];
  206.  
  207. console.log("Login failed with error code: " + code);
  208. failure(message);
  209. }
  210. }, 1000);
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement