public IHttpActionResult Login(LoginViewModel model) { try { if (!ModelState.IsValid) { return(Ok(new { success = false, token = new TokenModel { }, message = "Thông tin không hợp lệ" })); } var identity = PTIdentity.GetPTIdentity(model.UserName, model.Password); switch (PTIdentity.Status) { case LoginStatus.Success: { if (identity.Roles != null) { foreach (var info in identity.Roles) { model.Roles += info + ","; } } else if (string.IsNullOrEmpty(model.Roles)) { model.Roles = ","; } FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1, model.UserName, DateTime.Now, DateTime.Now.AddHours(FormsAuthentication.Timeout.Minutes), model.RememberMe, model.Roles, FormsAuthentication.FormsCookiePath); string encryptedTicket = FormsAuthentication.Encrypt(ticket); MemoryCacher.Add(model.UserName, encryptedTicket, DateTime.Now.AddDays(1)); //Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)); var token = new TokenModel { Token = JwtManager.GenerateToken(model.UserName, 60), Expiration = DateTime.UtcNow.AddMinutes(60) }; if (model.ReturnUrl.Length > 1 && model.ReturnUrl.StartsWith("/") && !model.ReturnUrl.StartsWith("//") && !model.ReturnUrl.StartsWith("/\\")) { return(Json(new { success = true, token = token, message = "" })); } else { return(Json(new { success = true, token = token, message = "" })); } } case LoginStatus.IsNotApproved: return(Json(new { success = false, token = new TokenModel { }, message = "Tài khoản chưa được xác nhận." })); case LoginStatus.LockedOut: return(Json(new { success = false, token = new TokenModel { }, message = "Tài khoản đã bị khóa." })); case LoginStatus.RequiresVerification: return(Json(new { success = false, token = new TokenModel { }, message = "Tài khoản chưa kích hoạt tài khoản." })); case LoginStatus.Failure: default: return(Json(new { success = false, token = new TokenModel { }, message = "Tên đăng nhập hoặc mật khẩu không đúng." })); } } catch (Exception ex) { return(Json(new { success = false, token = new TokenModel { }, message = ex.Message })); } }
public JsonResult Login(LoginViewModel model) { try { if (!ModelState.IsValid) { return(Json(new { success = false })); } var identity = PTIdentity.GetPTIdentity(model.UserName, model.Password); switch (PTIdentity.Status) { case LoginStatus.Success: { if (!string.IsNullOrEmpty(PTIdentity.PatientCode)) { model.Roles = Consultation.Utilities.Constants.B_Patient_Get + ","; } if (identity.Roles != null) { foreach (var info in identity.Roles) { model.Roles += info + ","; } } else if (string.IsNullOrEmpty(model.Roles)) { model.Roles = ","; } FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1, model.UserName, DateTime.Now, DateTime.Now.AddHours(FormsAuthentication.Timeout.Minutes), model.RememberMe, model.Roles, FormsAuthentication.FormsCookiePath); string encryptedTicket = FormsAuthentication.Encrypt(ticket); Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)); //if(!string.IsNullOrEmpty(PTIdentity.PatientCode)) model.ReturnUrl = "/benh-nhan-xem/" + PTIdentity.PatientCode + ".html"; if (Url.IsLocalUrl(model.ReturnUrl) && model.ReturnUrl.Length > 1 && model.ReturnUrl.StartsWith("/") && !model.ReturnUrl.StartsWith("//") && !model.ReturnUrl.StartsWith("/\\")) { return(Json(new { success = true, url = model.ReturnUrl })); } else { return(Json(new { success = true, url = string.Empty })); } } case LoginStatus.LockedOut: return(Json(new { success = false, messageError = "Tài khoản đã bị khóa." })); case LoginStatus.RequiresVerification: return(Json(new { success = false, messageError = "Tài khoản chưa kích hoạt tài khoản." })); case LoginStatus.Failure: default: return(Json(new { success = false, messageError = "Tên đăng nhập hoặc mật khẩu không đúng." })); } } catch (Exception ex) { ViewBag.StatusMessage = ex.Message; return(Json(new { success = false, error = ex.Message })); } }