コード例 #1
0
        public ActionResult GetJwtStr([FromBody] Crm_CustomerDto theData)
        {
            //这里就是用户登陆以后,通过数据库去调取数据,分配权限的操作
            var customer = _crm_CustomerService.GetUserTypeName(theData);

            if (customer != null)
            {
                var user = new UserEntities()
                {
                    userId    = customer.CustomerId,
                    userName  = customer.NickName,
                    userNo    = customer.No,
                    loginTime = DateTime.Now
                };

                TokenModelJwt tokenModel = new TokenModelJwt {
                    Uid = customer.CustomerId, Role = customer.UserTypeValue
                };

                user.token = JwtHelper.IssueJwt(tokenModel);
                return(Success(user));
            }
            else
            {
                return(Error("登录失败"));
            }
        }
コード例 #2
0
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="theData">登录信息</param>
        public Crm_CustomerDto CustomerLogin(Crm_CustomerDto dto)
        {
            dto.Password = dto.Password.ToMD5String();
            var result = GetIQueryable().Where(f => f.Name == dto.Name && f.Password == dto.Password).FirstOrDefault().MapTo <Crm_CustomerDto>();

            if (result.IsNullOrEmpty())
            {
                throw new Exception("登录失败!");
            }

            return(result);
        }
コード例 #3
0
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="theData">保存的数据</param>
        public ActionResult SaveData(Crm_CustomerDto theData)
        {
            if (theData.CustomerId.IsNullOrEmpty())
            {
                _crm_CustomerService.AddData(theData);
            }
            else
            {
                _crm_CustomerService.UpdateData(theData);
            }

            return(Success());
        }
コード例 #4
0
        /// <summary>
        /// 获取用户类型名称
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public Crm_CustomerDto GetUserTypeName(Crm_CustomerDto dto)
        {
            dto.Password = dto.Password.ToMD5String();
            var customer = _crm_CustomerService.GetIQueryable().Where(f => f.Name == dto.Name && f.Password == dto.Password).FirstOrDefault().MapTo <Crm_CustomerDto>();

            if (customer.IsNullOrEmpty())
            {
                throw new Exception("登录失败!");
            }

            customer.UserTypeValue = EnumExtension.GetEnumDescription(((EnumCustomerType)Enum.ToObject(typeof(EnumCustomerType), customer.UserType)));

            return(customer);
        }
コード例 #5
0
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="newData">数据</param>
        public int AddData(Crm_CustomerDto newData)
        {
            newData.CustomerId = Guid.NewGuid().ToSequentialGuid();
            newData.CreateTime = DateTime.Now;
            newData.Password   = newData.Password.Trim().ToMD5String();
            newData.No         = NoExtension.ShortNo(Guid.NewGuid().GetHashCode().ToString());

            var result = Insert(newData);

            if (result == 0)
            {
                throw new Exception("添加失败!");
            }

            return(result);
        }
コード例 #6
0
        /// <summary>
        /// 获取用户类型名称
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public dynamic RefreshToken(Crm_CustomerDto dto)
        {
            try
            {
                string jwtStr = string.Empty;
                bool   suc    = false;

                if (string.IsNullOrEmpty(dto.Token))
                {
                    throw new Exception("token无效,请重新登录!");
                }

                var tokenModel = JwtHelper.SerializeJwt(dto.Token);
                if (tokenModel != null && !tokenModel.Uid.IsNullOrEmpty())
                {
                    throw new Exception("token无效,请重新登录!");
                }

                var customer = _crm_CustomerService.GetIQueryable().Where(f => f.Name == dto.Name && f.Password == dto.Password).FirstOrDefault().MapTo <Crm_CustomerDto>();

                if (customer.IsNullOrEmpty())
                {
                    throw new Exception("登录失败!");
                }

                customer.UserTypeValue = EnumExtension.GetEnumDescription(((EnumCustomerType)Enum.ToObject(typeof(EnumCustomerType), customer.UserType)));

                //如果是基于用户的授权策略,这里要添加用户;如果是基于角色的授权策略,这里要添加角色
                var claims = new List <Claim> {
                    new Claim(ClaimTypes.Name, customer.Name),
                    new Claim(JwtRegisteredClaimNames.Jti, tokenModel.Uid.ObjToString()),
                    new Claim(ClaimTypes.Expiration, DateTime.Now.AddSeconds(_requirement.Expiration.TotalSeconds).ToString())
                };
                claims.AddRange(customer.UserTypeValue.Split(',').Select(s => new Claim(ClaimTypes.Role, s)));

                //用户标识
                var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme);
                identity.AddClaims(claims);

                var refreshToken = JwtToken.BuildJwtToken(claims.ToArray(), _requirement);
                return(refreshToken);
            }
            catch (Exception)
            {
                throw new Exception("认证失败!");
            }
        }
コード例 #7
0
        /// <summary>
        /// 更新数据
        /// </summary>
        public int UpdateData(Crm_CustomerDto theData)
        {
            var theUser = GetEntity(theData.CustomerId);

            if (theData.Password.Trim() != theUser.Password)
            {
                theData.Password = theData.Password.Trim().ToMD5String();
            }
            var result = Modify(theData, "Password", "UserType", "Status");

            if (result == 0)
            {
                throw new Exception("更新失败!");
            }

            return(result);
        }
コード例 #8
0
        /// <summary>
        /// 获取用户类型名称
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public dynamic GetJwtToken3(Crm_CustomerDto dto)
        {
            try
            {
                if (string.IsNullOrEmpty(dto.Name) || string.IsNullOrEmpty(dto.Password))
                {
                    throw new Exception("用户名或密码不能为空!");
                }
                dto.Password = dto.Password.ToMD5String();
                var customer = _crm_CustomerService.GetIQueryable().Where(f => f.Name == dto.Name && f.Password == dto.Password).FirstOrDefault().MapTo <Crm_CustomerDto>();

                if (customer.IsNullOrEmpty())
                {
                    throw new Exception("登录失败!");
                }

                customer.UserTypeValue = EnumExtension.GetEnumDescription(((EnumCustomerType)Enum.ToObject(typeof(EnumCustomerType), customer.UserType)));


                //如果是基于用户的授权策略,这里要添加用户;如果是基于角色的授权策略,这里要添加角色
                var claims = new List <Claim> {
                    new Claim(ClaimTypes.Name, dto.Name),
                    new Claim(JwtRegisteredClaimNames.Jti, customer.CustomerId.ToString()),
                    new Claim(ClaimTypes.Expiration, DateTime.Now.AddSeconds(_requirement.Expiration.TotalSeconds).ToString())
                };
                claims.AddRange(customer.UserTypeValue.Split(',').Select(s => new Claim(ClaimTypes.Role, s)));

                //用户标识
                var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme);
                identity.AddClaims(claims);

                var token = JwtToken.BuildJwtToken(claims.ToArray(), _requirement);
                return(token);
            }
            catch (Exception ex)
            {
                throw new Exception("认证失败!");
            }
        }
コード例 #9
0
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="theData">删除的数据</param>
        public ActionResult CustomerLogin(Crm_CustomerDto theData)
        {
            theData = _crm_CustomerService.CustomerLogin(theData);

            return(Success("登录成功!", theData));
        }
コード例 #10
0
        public JsonResult RefreshToken([FromBody] Crm_CustomerDto theData)
        {
            var token = _crm_CustomerService.RefreshToken(theData);

            return(new JsonResult(token));
        }
コード例 #11
0
        public JsonResult GetJwtToken3([FromBody] Crm_CustomerDto theData)
        {
            var token = _crm_CustomerService.GetJwtToken3(theData);

            return(new JsonResult(token));
        }
コード例 #12
0
        public ActionResult UserLogin([FromForm] Crm_CustomerDto theData)
        {
            theData = _crm_CustomerService.CustomerLogin(theData);

            return(Ok((Crm_CustomerDto)theData));
        }