public ActionResult NewUser(FormCollection formData) { string name, email, phone, msg = string.Empty; name = Convert.ToString(formData["UserName"]); email = Convert.ToString(formData["Mail"]); phone = Convert.ToString(formData["Mobile"]); try { if (ModelState.IsValid) { OTCUsersModel otcUseer = new OTCUsersModel(); if (!otcUseer.IsUserExist(email)) { msg = otcUseer.CreateUseer(name, email, phone); } else { msg = string.Format("User recode already exist with email address {0}", email); } ViewBag.Message = msg; } } catch (Exception ex) { ViewBag.Error = ex.Message; } return(View()); }
public ActionResult Login(FormCollection formData) { OTCUsersModel userModel = new OTCUsersModel(); string username = Convert.ToString(formData["UserLogin"]); string password = Convert.ToString(formData["UserPassword"]); string userInfo = userModel.ValidateUser(username, password); if (userInfo.Trim().Length > 0) { if (userInfo == "X") { ViewBag.Message = "Your account got locked. Please contact admin!"; } else if (userInfo == "O") { ViewBag.Message = "Invalid User Name or Password."; } else if (userInfo.Trim().Length > 10) //Asume valid userInfo will have more than 10 lenth { var UserDetails = userInfo.Split('|'); CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel(); serializeModel.UserID = Convert.ToInt32(UserDetails[0]); serializeModel.Name = UserDetails[1]; serializeModel.UserName = UserDetails[2]; serializeModel.Role = UserDetails[3]; string userData = JsonConvert.SerializeObject(serializeModel); FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 1, UserDetails[0], DateTime.Now, DateTime.Now.AddMinutes(15), false, userData); string encTicket = FormsAuthentication.Encrypt(authTicket); HttpCookie faCookie = new HttpCookie(SampleHelper.GetAuthCookieName(), encTicket); Response.Cookies.Add(faCookie); if (serializeModel.Role == "User") { return(RedirectToAction("Index", "Student")); } else if (serializeModel.Role == "Admin") { return(RedirectToAction("Index", "Admin")); //return RedirectToAction("NewUser", "OTC"); } } } //return RedirectToAction("Index", "Login"); return(Login()); }