Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @using Project.Models
- @model LoginInfo
- <head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width" />
- <title>Login</title>
- <script src="~/Scripts/jquery-2.2.0.js"></script>
- <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
- <script src="/Scripts/jquery.validate.min.js"></script>
- <script src="/Scripts/jquery.validate.unobtrusive.min.js"></script>
- <script src="~/Content/Scripts/login.js"></script>
- <script>
- $('html').removeClass('nojs');
- </script>
- </head>
- <body>
- <div id="container">
- @using (Ajax.BeginForm("Ajax", "Login", new AjaxOptions { OnBegin = "submit", OnComplete = "response" }))
- {
- <img id="logo" src="~/Content/Images/Main.png" alt="" />
- <table>
- <tr>
- <td>
- @Html.LabelFor(x => x.serverHost)
- </td>
- <td>
- @Html.TextBoxFor(x => x.serverHost, new { placeholder = "domain", required = "required" })
- </td>
- </tr>
- <tr>
- <td>
- @Html.LabelFor(x => x.instanceName)
- </td>
- <td>
- @Html.TextBoxFor(x => x.instanceName, new { placeholder = "company", required = "required" })
- </td>
- </tr>
- <tr>
- <td>
- @Html.LabelFor(x => x.username)
- </td>
- <td>
- @Html.TextBoxFor(x => x.username, new { placeholder = "Username", required = "required" })
- </td>
- </tr>
- <tr>
- <td>
- @Html.LabelFor(x => x.password)
- </td>
- <td>
- @Html.PasswordFor(x => x.password, new { placeholder = "Password", required = "required" })
- </td>
- </tr>
- </table>
- <table>
- <tr>
- <td id="remembertd">
- @Html.LabelFor(x => x.remember, "Remember me")
- @Html.CheckBoxFor(x => x.remember, new { @checked = Model.remember })
- </td>
- <td>
- <input id="submit" type="submit" value="Connect" />
- </td>
- </tr>
- </table>
- }
- </div>
- </body>
- </html>
- [AllowAnonymous]
- public class LoginController : Controller
- {
- public ActionResult Index()
- {
- return View(new LoginInfo());
- }
- [ValidateInput(true)]
- public ActionResult Ajax(LoginInfo info)
- {
- HttpStatusCodeResult result = new HttpStatusCodeResult(200);
- if (!ModelState.IsValid)
- {
- result = GetFailedResult(new ModelStateInvalidException(), Reason.ModelStateInvalid);
- }
- else
- {
- try
- {
- var i = 0;
- var j = 10 / i;
- }
- catch (Exception ex)
- {
- result = GetFailedResult(ex, Reason.Unhandled);
- }
- }
- return new HttpStatusCodeResult(result.StatusCode, result.StatusDescription);
- }
- private HttpStatusCodeResult GetFailedResult(Exception ex, Reason reason)
- {
- HttpStatusCode httpErrorCode = HttpStatusCode.InternalServerError;
- String errorMessage = String.Empty;
- //Trace
- RemotingHelper.Trace("Login attempt failed with message: " + ex.Message + "n----------");
- //Get message/code
- switch (reason)
- {
- case Reason.IncorrectInstanceName:
- case Reason.IncorrectServerHost:
- case Reason.IncorrectServerHostPort:
- case Reason.IncorrectUsernameOrPassword:
- httpErrorCode = HttpStatusCode.Forbidden;
- errorMessage = "Could not connect using the provided details.";
- break;
- case Reason.Offline:
- httpErrorCode = HttpStatusCode.ServiceUnavailable;
- errorMessage = "Could not establish connection with the server.";
- break;
- case Reason.LicenseMissing:
- httpErrorCode = HttpStatusCode.InternalServerError;
- errorMessage = "There was a problem with your license.nPlease contact an administrator.";
- break;
- default:
- httpErrorCode = HttpStatusCode.InternalServerError;
- errorMessage = "There was an error attempting to log in.nPlease contact an administrator.";
- break;
- }
- //Return
- return new HttpStatusCodeResult(httpErrorCode, errorMessage + ":" + ((Int32)reason).ToString());
- }
- }
- public enum Reason
- {
- Network = 10,
- Offline = 11,
- LicenseMissing = 12,
- LicenseFileMissing,
- NullChannel = 14,
- ModelStateInvalid = 15,
- Unhandled = 16,
- IncorrectServerHost = 17,
- IncorrectServerHostPort = 18,
- IncorrectInstanceName = 19,
- IncorrectUsernameOrPassword = 20
- }
- public class LoginInfo
- {
- public LoginInfo()
- {
- serverHost = string.Empty;
- instanceName = string.Empty;
- username = string.Empty;
- password = string.Empty;
- remotingPassword = null;
- remember = false;
- }
- [Required]
- [Display(Name = "Server Host")]
- public string serverHost { get; set; }
- [Required]
- [Display(Name = "Instance Name")]
- public string instanceName { get; set; }
- [Required]
- [Display(Name = "Username")]
- public string username { get; set; }
- [Required]
- [Display(Name = "Password")]
- public string password { get; set; }
- [Display(Name = "Remoting Password")]
- public string remotingPassword { get; set; }
- [Display(Name = "Persistent")]
- public bool remember { get; set; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement