Exemplo n.º 1
0
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                AccountContext dbContext = new AccountContext();
                User userObj = dbContext.Login(model.UserName, model.Password);
                if (userObj != null)
                {
                    //List<userrole> roles = dbContext.GetRoles(personObj.userid);                    
                    //System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
                    //string jsstr = js.Serialize(roles); 
                    if (userObj.enable == (int)EnableEnum.DISABLE)
                    {
                        ModelState.AddModelError("", "该用户被禁止登录,请联系管理员。");
                        return View(model);  
                    }

                    int expiration = 0;
                    if (!int.TryParse(System.Configuration.ConfigurationManager.AppSettings["Expiration"].ToString(), out expiration))
                    {
                        expiration = 30;
                    }

                    String userData = "";
                    if( userObj.usertype == ((int)UserTypeEnum.ADMIN).ToString()) { userData = "admin"; }
                    else if( userObj.usertype ==( (int)UserTypeEnum.QUERY).ToString()) { userData = "query"; }

                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, model.UserName, DateTime.Now,
                        DateTime.Now.AddMinutes(expiration), false, userData);       

                    string ticketEncrypt = FormsAuthentication.Encrypt(ticket);
                    System.Web.HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypt);
                    System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
                   
                   

                    return RedirectToLocal(returnUrl);
                }
                else
                {
                    ModelState.AddModelError("", "提供的用户名或密码不正确。");
                    return View(model);
                }
            }

            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            ModelState.AddModelError("", "提供的用户名或密码不正确。");
            return View(model);
        }
Exemplo n.º 2
0
        public JsonResult LoginRestfull(string userName, string password)
        {
            try
            {
                if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
                {
                    Result result = new Result((int)ResultCodeEnum.Error, "用户名或密码空!", null);
                    JsonResult jr = new JsonResult();
                    jr.Data = result;
                    jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                    return jr;
                }
                AccountContext dbContext = new AccountContext();
                ContractMvcWeb.Models.Beans.User userObj = dbContext.Login(userName, password);
                if (userObj != null)
                {
                    //List<userrole> roles = dbcontext.GetRoles(userobj.userid);
                    //System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
                    //string jsstr = js.Serialize(roles);
                    Result result = null;
                    JsonResult jr = null;
                    if (userObj.enable == (int)EnableEnum.DISABLE)
                    {
                        result = new Result((int)ResultCodeEnum.Error ,"该用户被禁止登录。",null);
                        jr = new JsonResult();
                        jr.Data = result;
                        return jr;
                    }

                    int expiration = 0;
                    if (!int.TryParse(System.Configuration.ConfigurationManager.AppSettings["Expiration"].ToString(), out expiration))
                    {
                        expiration = 30;
                    }

                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now,
                        DateTime.Now.AddMinutes(expiration), false, string.Empty);

                    string ticketEncrypt = FormsAuthentication.Encrypt(ticket);
                    System.Web.HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypt);
                    System.Web.HttpContext.Current.Response.Cookies.Add(cookie);

                    result = new Result((int)ResultCodeEnum.Success, "", userObj);
                    jr = new JsonResult();
                    jr.Data = result;
                    return jr;
                }
                else
                {
                    Result result = new Result((int)ResultCodeEnum.Error, "用户名或密码错误!", null);
                    JsonResult jResult = new JsonResult();
                    jResult.Data = result;
                    return jResult;
                }
            }
            catch (Exception ex)
            {
                JsonResult jr = new JsonResult();
                jr.Data = new Result((int)ResultCodeEnum.Error, ex.Message, "");
                return jr;
            }
        }