Пример #1
0
        public ActionResult Login(LoginDto dto)
        {
            dto.CheckNotNull("dto");
            OperationResult result = new OperationResult(OperationResultType.ValidError);

            if (ModelState.IsValid)
            {
                try
                {
                    if (Session["ValidateCode"] == null || !dto.CheckCode.ToLower().Equals(Session["ValidateCode"].ToString().ToLower()))
                    {
                        ModelState.AddModelError("CheckCode", "验证码不正确!");
                    }
                    else
                    {
                        //CommunicationCryptor cryptor = new CommunicationCryptor("", "", "SHA1");
                        //dto.LoginPwd = cryptor.EncryptData(dto.LoginPwd);
                        result = IdentityContract.CheckLogin(dto);
                        if (result.ResultType == OperationResultType.Success)
                        {
                            User user = result.Data as User;

                            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                            AuthenticationManager.SignIn(new AuthenticationProperties()
                            {
                                IsPersistent = false
                            },
                                                         new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie).SetClaimsIdentity(user.Id.ToString(), user.UserName, user.NickName, null));
                            return(RedirectToAction("Index", "Home", new { }));
                        }
                        else
                        {
                            ModelState.AddModelError("LoginName", result.Message);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("Exception", ex.Message);
                }
            }
            ViewBag.ErrorsMessage = GetModelErrors(ModelState);
            return(View(dto));
        }