コード例 #1
0
        public async Task <ApiResult <LoginSysUserDto> > Get(string username, string password)
        {
            ApiResult <LoginSysUserDto> res = new ApiResult <LoginSysUserDto>();

            res.data = new LoginSysUserDto();
            //获取用户信息
            ApiResult <SysUser> apiResult = await _userService.UserNameAndPassQueryAsync(username, password);

            if (apiResult.code != (int)ApiEnum.Status)
            {
                res.code    = apiResult.code;
                res.message = apiResult.message;
                return(await Task.Run(() => res));
            }

            //修改登录信息
            var userLogonUp = await _userLogonService.UpdateAsync(c => new SysUserLogon {
                LogOnCount = c.LogOnCount + 1
            }, s => s.UserId == apiResult.data.Id);

            if (userLogonUp)
            {
                string ExpirationTime = DateTime.Now.AddSeconds(_requirement.Expiration.TotalSeconds).ToString();

                var claims = new List <Claim> {
                    new Claim(JwtRegisteredClaimNames.Jti, apiResult.data.Id),
                    new Claim(ClaimTypes.Name, apiResult.data.RealName),
                    new Claim(ClaimTypes.Gender, "Web"),
                    new Claim(ClaimTypes.GroupSid, apiResult.data.OrganizeId),
                    new Claim(ClaimTypes.Authentication, apiResult.data.IsAdministrator == true ? "1" : "0"),
                    new Claim(ClaimTypes.Expiration, ExpirationTime)
                };

                claims.Add(new Claim(ClaimTypes.Role, apiResult.data.RoleId));
                //用户标识
                var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme);
                identity.AddClaims(claims);
                res.data.token   = JwtToken.BuildJwtToken(claims.ToArray(), _requirement).token;
                res.data.expires = ExpirationTime;


                //返回过期时间
            }

            return(await Task.Run(() => res));
        }
コード例 #2
0
ファイル: SysUserController.cs プロジェクト: ztxyzu/BasisSoa
        public async Task <ApiResult <string> > Put(string Id, EditSysUserDto Params)
        {
            ApiResult <string> res = new ApiResult <string>();


            //开启事务
            try
            {
                SysUser userInfo = _mapper.Map <SysUser>(Params);
                if (!UniversalTool.ModuleIsNull(userInfo))
                {
                    userInfo.Id = Id;
                    await _userService.UpdateAsync(userInfo);
                }


                if (string.IsNullOrEmpty(Params.UserPassword) || Params.UserPassword == "********")
                {
                    Params.UserPassword     = null;
                    Params.PasswordSecurity = null;
                }
                SysUserLogon userLogonInfo = _mapper.Map <SysUserLogon>(Params);
                if (!UniversalTool.ModuleIsNull(userLogonInfo))
                {
                    SysUserLogon sysUserLogon = await _userLogonService.QueryFirstAsync(s => s.UserId == Id);

                    if (!string.IsNullOrEmpty(Params.UserPassword) && Params.UserPassword != "********")
                    {
                        userLogonInfo.PasswordSecurity = UniversalTool.PassSecurityValidation(userLogonInfo.UserPassword);
                        userLogonInfo.UserSecretkey    = Md5Crypt.Encrypt(Guid.NewGuid().ToString());
                        userLogonInfo.UserPassword     = Md5Crypt.Encrypt(DES3Encrypt.EncryptString(userLogonInfo.UserPassword.ToLower(), userLogonInfo.UserSecretkey).ToLower(), false).ToLower();
                    }
                    userLogonInfo.Id = sysUserLogon.Id;
                    await _userLogonService.UpdateAsync(userLogonInfo);
                }
            }
            catch (Exception ex)
            {
                res.code    = (int)ApiEnum.Error;
                res.message = "异常:" + ex.Message;
            }

            //事务结束

            return(await Task.Run(() => res));
        }