Beispiel #1
0
        public async Task <ActionResult> CheckLogin(string username, string password, string code)
        {
            //初始化登录日志
            LogEntity logEntity = new LogEntity();

            logEntity.ModuleName = "系统登录";
            logEntity.Type       = DbLogType.Login.ToString();
            try
            {
                //验证 '验证码'
                var verifyCodeInSession = WebHelper.GetSession("evolution_session_verifycode", HttpContext);
                if (verifyCodeInSession.IsEmpty() || Md5.md5(code.ToLower(), 16) != verifyCodeInSession)
                {
                    throw new Exception("验证码错误,请重新输入!");
                }
                //验证用户名密码
                var userEntity = await userApp.CheckLogin(username, password);

                if (userEntity == null)
                {
                    throw new Exception("密码不正确,请重新输入");
                }
                var role = await roleApp.GetRoleById(userEntity.RoleId);

                //设置登录对象
                LoginModel operatorModel = CreateLoginModel(userEntity, role);
                //写入登录日志
                logEntity.Account     = userEntity.Account;
                logEntity.NickName    = userEntity.RealName;
                logEntity.Result      = true;
                logEntity.Description = "登录成功";
                await logApp.WriteDbLog(logEntity, HttpContext);

                //登录
                logonApp.SignIn(operatorModel, HttpContext);

                return(Content(new AjaxResult {
                    state = ResultType.success.ToString(), message = "登录成功。"
                }.ToJson()));
            }
            catch (Exception ex)
            {
                logEntity.Account     = username;
                logEntity.NickName    = username;
                logEntity.Result      = false;
                logEntity.Description = "登录失败," + ex.Message;
                await logApp.WriteDbLog(logEntity, HttpContext);

                return(Content(new AjaxResult {
                    state = ResultType.error.ToString(), message = ex.Message
                }.ToJson()));
            }
        }
Beispiel #2
0
        public async Task <ActionResult> CheckLoginJwt(string username, string password, string code, string tid)
        {
            //初始化登录日志
            LogEntity logEntity = new LogEntity();

            logEntity.ModuleName = "系统登录";
            logEntity.Type       = DbLogType.Login.ToString();
            UserEntity userEntity = null;

            try
            {
                //验证用户名密码
                HttpClient _client = new HttpClient();
                //arrange
                var data = new Dictionary <string, string>();
                data.Add("username", username);
                data.Add("password", password);
                string              url           = config["ApiServerBaseUrl"];
                HttpContent         ct            = new FormUrlEncodedContent(data);
                HttpResponseMessage message_token = _client.PostAsync(url + "/auth/token", ct).Result;
                string              res           = message_token.Content.ReadAsStringAsync().Result;
                Token token = Newtonsoft.Json.JsonConvert.DeserializeObject <Token>(res);

                if (token != null)
                {
                    userEntity = userApp.GetEntityByName(username, tid).Result;
                }

                //var userEntity = await userApp.CheckLogin(username, password);
                if (userEntity == null)
                {
                    throw new Exception("密码不正确,请重新输入");
                }
                var role = await roleApp.GetRoleById(userEntity.RoleId, tid);

                //设置登录对象
                LoginModel operatorModel = CreateLoginModel(userEntity, role);
                //写入登录日志
                logEntity.Account     = userEntity.Account;
                logEntity.NickName    = userEntity.RealName;
                logEntity.Result      = true;
                logEntity.Description = "登录成功";
                await logApp.WriteDbLog(logEntity, userEntity.Id);

                //登录
                logonApp.SignIn(operatorModel, HttpContext);
                HttpContext.Response.Cookies.Append("access_token", token.access_token);
                return(Content(new AjaxResult {
                    state = ResultType.success.ToString(), message = "登录成功。"
                }.ToJson()));
            }
            catch (Exception ex)
            {
                logEntity.Account     = username;
                logEntity.NickName    = username;
                logEntity.Result      = false;
                logEntity.Description = "登录失败," + ex.Message;
                await logApp.WriteDbLog(logEntity, userEntity.Id);

                return(Content(new AjaxResult {
                    state = ResultType.error.ToString(), message = ex.Message
                }.ToJson()));
            }
        }