Пример #1
0
        public ActionResult Login(LoginViewModel model)
        {
            //验证模型
            if (ModelState.IsValid)
            {
                //查询
                var user = new DbEntities <User>().SimpleClient.GetList().Where(u => u.UserName == model.LoginUserName && u.UserPasswd == MD5PWD.GetMD5PWD(model.LoginUserPW)).FirstOrDefault();

                if (user != null)
                {
                    //该用户是否经过审核
                    if (user.IsChecked)
                    {
                        //清空Cookie
                        ClearCookie();

                        //根据当前用户的id获取用户所在用户组的级别--用于判断是否有权限访问
                        var userGroup = new DbEntities <UserGroup>().SimpleClient.GetById(user.UserGroupID);

                        if (userGroup != null)
                        {
                            //生成用户验证信息模型
                            IdentityInfoModel infoModel = new IdentityInfoModel
                            {
                                CurUserID         = user.UserID,
                                CurUserGroupID    = userGroup.UserGroupID,
                                CurUserGroupClass = (int)userGroup.UserGroupClass
                            };

                            //初始化凭据-为forms提供用户身份的票证,有效期六个小时
                            FormsAuthenticationTicket authenticationTicket = new FormsAuthenticationTicket(1, model.LoginUserName, DateTime.Now, DateTime.Now.AddHours(6), false, new JavaScriptSerializer().Serialize(infoModel));

                            //加密该用户凭证
                            string encryptedTicket = FormsAuthentication.Encrypt(authenticationTicket);

                            //保存在Cookie中
                            HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                            HttpContext.Response.Cookies.Add(authCookie);

                            //重定向到主页
                            return(RedirectToAction("Index", "Home", new { area = "" }));
                        }
                    }
                    else
                    {
                        //该用户未被审核-添加错误信息
                        ModelState.AddModelError("LoginUserName", $"用户{model.LoginUserName}为经过审核");
                    }
                }
                else
                {
                    //找不到该用户信息-添加错误信息
                    ModelState.AddModelError("LoginUserPW", "用户名不存在或密码错误");
                }
            }
            return(View(model));
        }
Пример #2
0
        public RedirectResult Auth(string code, string error)
        {
            UserInfo          userInfo = CurrentClient.GetUserInfo(Request.QueryString);
            IdentityInfoModel info     = DataService.AuthorizeUser(userInfo, User.UserId);

            if (!User.Identity.IsAuthenticated)
            {
                SocialIdentity.SetAuthTicket(info, Response);
            }
            return(GetRedirectToSourceUrl());
        }
Пример #3
0
        public static void SetAuthTicket(IdentityInfoModel info, HttpResponseBase context)
        {
            var ticketData = new NameValueCollection
            {
                { "firstName", info.FirstName },
                { "lastName", info.LastName },
                { "email", info.Email },
                { "avatarUpdated", info.AvatarUpdated.Ticks.ToString() }
            };

            new FormsAuthentication().SetAuthCookie(context, info.Id.ToString(), true, ticketData);
        }