Beispiel #1
0
        public IHttpActionResult Login(dynamic request)
        {
            dynamic result = new { code = 0, success = 1, msg = "登录失败" };

            try
            {
                if (!string.IsNullOrWhiteSpace(request.UserName.ToString()) && !string.IsNullOrWhiteSpace(request.Password.ToString()))
                {
                    string userName = request.UserName.ToString().ToLower();
                    string password = request.Password.ToString().ToLower();

                    int userId = -1;
                    if ("superadmin" == userName && password == ConfigHelper.Instance.WebSiteConfig["SuperPassword"])
                    {
                        userId = 1;
                    }
                    else
                    {
                        userId = UserService.Instance.UserLogin(userName, password);
                    }

                    if (userId > -1)
                    {
                        int tokenTimeLimit = int.Parse(ConfigHelper.Instance.WebSiteConfig["TokenTimeLimit"]);

                        WJ_T_Token tokenInfo = new WJ_T_Token();
                        tokenInfo.UserId           = userId;
                        tokenInfo.Token_Ip         = System.Web.HttpContext.Current.Request.UserHostAddress;
                        tokenInfo.Token_CreateTime = DateTime.Now;
                        tokenInfo.Token_TimeLimit  = DateTime.Now.AddSeconds(tokenTimeLimit);

                        TokenService.Instance.Add(tokenInfo);

                        AuthInfo authInfo = new AuthInfo()
                        {
                            UserId         = userId,
                            IsSuperAdmin   = userId == 0,
                            CreateTime     = tokenInfo.Token_CreateTime,
                            TokenTimeLimit = tokenInfo.Token_TimeLimit
                                             //,RoleMenu = UserService.Instance.GetUserControllerName(userId)
                        };
                        string token = JWTService.Instance.CreateToken(authInfo);
                        result = new { code = 0, success = 0, data = new { access_token = token } };
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(Json <dynamic>(result));
        }
Beispiel #2
0
        public IHttpActionResult Login(JObject request)
        {
            ResultModel resultObj = GetResultInstance();

            try
            {
                if (IsPropertyExist(request, "UserName") && IsPropertyExist(request, "Password"))
                {
                    string userName = request["UserName"].ToString().Trim().ToLower();
                    string password = request["Password"].ToString().Trim().ToLower();

                    int userId = UserService.Instance.UserLogin(userName, password);

                    if (userId > -1)
                    {
                        // Token有效期
                        int tokenTimeLimit = SystemMapService.Instance.GetMapValueToInt("TokenTimeLimit");

                        string prefix = SystemMapService.Instance.GetMapValue("TokenPrefix");
                        string token  = prefix + Guid.NewGuid().ToString().Replace("-", "");

                        UserService.Instance.UpdateUserToken(userId, token);//更新用户Token

                        #region 保存Token信息
                        WJ_T_Token tokenInfo = new WJ_T_Token();
                        tokenInfo.UserId           = userId;
                        tokenInfo.Token_Ip         = ((System.Web.HttpContextWrapper)Request.Properties["MS_HttpContext"]).Request.UserHostAddress;
                        tokenInfo.Token_Value      = token;
                        tokenInfo.Token_CreateTime = DateTime.Now;
                        tokenInfo.Token_TimeLimit  = DateTime.Now.AddSeconds(tokenTimeLimit);
                        TokenService.Instance.Add(tokenInfo);
                        #endregion

                        SetSuccessResult(resultObj, new { Access_Token = token });
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                LogHelper.DebugLog(ex.Message, LogType.Controller);
                SetFailResult(resultObj, ex.Message);
            }

            return(Json <dynamic>(resultObj));
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public ResultModel UserLogin(dynamic requestData)
        {
            ResultModel resultObj = new ResultModel {
                Success = 0, Code = 0, ErrorMsg = "登录失败"
            };

            try
            {
                string userName = requestData.UserName.ToString().ToLower();
                string password = requestData.Password.ToString().ToLower();

                int userId = UserService.Instance.UserLogin(userName, password);

                if (userId > -1)
                {
                    // Token有效期
                    int tokenTimeLimit = SystemMapService.Instance.GetMapValueToInt("TokenTimeLimit");

                    string prefix = SystemMapService.Instance.GetMapValue("TokenPrefix1");
                    string token  = prefix + Guid.NewGuid().ToString().Replace("-", "");

                    UserService.Instance.UpdateUserToken(userId, token);

                    WJ_T_Token tokenInfo = new WJ_T_Token();
                    tokenInfo.UserId           = userId;
                    tokenInfo.Token_Ip         = ((System.Web.HttpContextWrapper)Request.Properties["MS_HttpContext"]).Request.UserHostAddress;
                    tokenInfo.Token_Value      = token;
                    tokenInfo.Token_CreateTime = DateTime.Now;
                    tokenInfo.Token_TimeLimit  = DateTime.Now.AddSeconds(tokenTimeLimit);
                    TokenService.Instance.Add(tokenInfo);

                    SetSuccessResult(resultObj, new { Access_Token = token });
                }
            }
            catch (Exception ex)
            {
                resultObj.ErrorMsg = ex.Message;
                LogHelper.ControllerErrorLog(ex.Message);
            }
            return(resultObj);
        }