Beispiel #1
0
        public ActionResult SignIn(InputSignIn input)
        {
            if (IsGetRequest)
            {
                return(View());
            }
            var result = new BaseOutput();

            if (string.IsNullOrEmpty(input.UserName))
            {
                SetResponse(s => s.NameEmpty, input, result);
            }
            else if (string.IsNullOrEmpty(input.Pwd))
            {
                SetResponse(s => s.PwdEmpty, input, result);
            }
            else
            {
                var repo    = GetRepo <User>();
                var level   = 0;
                var account = repo.GetFiltered(f => f.UserName == input.UserName, true).FirstOrDefault();
                if (account != null)
                {
                    if (account.Status == Status.Enabled)
                    {
                        var encryptedPwd = SHAEncrypt.SHA1(input.Pwd);
                        if (account.Pwd != encryptedPwd)
                        {
                            SetResponse(s => s.NamePwdNotMatch, input, result);
                        }
                        else
                        {
                            account.ModifiedAt = DateTime.Now;
                            result.data        = new { token = RandomGenerator.Next(20), url = UrlVar.Home_Index };
                            var expireAt = DateTime.Now.AddMinutes(AppConfig.GetValue <double>(GlobalVar.CacheMinute));
                            var userInfo = new UserInfo
                            {
                                UserId    = account.Id,
                                UserName  = input.UserName,
                                Roles     = account.Roles.Split(',').Select(s => int.Parse(s)).ToArray(),
                                Token     = result.data.token,
                                ExpiredAt = expireAt.ToString("yyyy-MM-dd HH:mm:ss"),
                                LoginIP   = HttpUtil.RequestHostAddress
                            };
                            userInfo.Level = RoleService.GetMaxLevel(userInfo.Roles);
                            userInfo.Auths = CommonService.GetAuths(userInfo.Roles);
                            userInfo.Urls  = CommonService.GetUrls(userInfo.Roles);
                            Resolve <ICache>().Set(CacheKey.GetUserKey(userInfo.UserId), userInfo, expireAt);
                            CookieUtil.WriteCookie(GlobalVar.CookieName, GlobalVar.UserId, userInfo.UserId.ToString());
                            CookieUtil.WriteCookie(GlobalVar.CookieName, GlobalVar.UserName, userInfo.UserName);
                            CookieUtil.WriteCookie(GlobalVar.CookieName, GlobalVar.Level, userInfo.Level.ToString());
                            CookieUtil.WriteCookie(GlobalVar.CookieName, GlobalVar.AuthToken, userInfo.Token);
                            level = userInfo.Level;
                            SetResponse(s => s.Success, input, result);
                        }
                    }
                    else
                    {
                        SetResponse(s => s.UserForbidden, input, result);
                    }
                }
                else
                {
                    SetResponse(s => s.NameNotExist, input, result);
                }
                LogService.AddLoginLog(new LoginLog {
                    UserName = input.UserName, LoginIP = HttpUtil.RequestHostAddress, Status = result.msg, Level = level
                });
            }
            return(JsonNet(result));
        }