Esempio n. 1
0
        protected async override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            HttpResponseMessage response = new HttpResponseMessage();

            string id = string.Empty;

            try
            {
                id = await AuthenticateWithThirdPartyApp(request);
            }
            catch (Exception ex)
            {
                ErrorOAtuhModel res = new ErrorOAtuhModel();
                res.error             = ErrorOAuthCode.Invalid_ThirdParty_OAuth;
                res.error_description = "第三方App认证失败";
                response = identityService.GenerateOAuthRespose(res);
                return(response);
            }
            bool loginRessult = userRepository.LoginApp(null, null, id, this.app_type);

            if (loginRessult)
            {
                var userRole = userRepository.GetUserByThirdPartyId(id, this.app_type);

                #region 当前第三方用户已经在本应用绑定过

                //根据绑定获取用户名和角色

                UserClaimsInfoModel userClaimsInfoModel = this.identityService.CreateUserClaimsInfoModelFromUserRepoModel(userRole);
                response = await identityService.OAuthSuccessResponse(userClaimsInfoModel);

                #endregion
            }
            else
            {
                #region 当前第三方用户尚未在本应用绑定过

                UnBindErrorOAuthModel res = new UnBindErrorOAuthModel();
                res.error             = ErrorOAuthCode.Invalid_bind_OAuth;
                res.error_description = "第三方App认证成功,但尚未与本地账号绑定";
                //要存储到redis中
                string relationAccountToken = Guid.NewGuid().ToString();
                RelationAccountModel relationAccountModel = new RelationAccountModel();
                relationAccountModel.from         = this.app_type;
                relationAccountModel.thirdPartyID = id;
                await relationAccountRepository.InsertRelationAccountModel(relationAccountToken, relationAccountModel, new TimeSpan(1, 0, 0));

                res.relationAccountID = relationAccountToken;
                response = identityService.GenerateOAuthRespose(res);


                #endregion
            }


            return(response);
        }
Esempio n. 2
0
        public async Task <bool> InsertRelationAccountModel(string relationtoken, RelationAccountModel relationAccountModel, TimeSpan?expiry)
        {
            Enforce.ArgumentNotNull <string>(relationtoken, "Relation Token 不能为null");
            Enforce.ArgumentNotNull <RelationAccountModel>(relationAccountModel, "RelationAccountModel 不能为null");

            string key    = string.Concat(prefix, relationtoken);
            string value  = Newtonsoft.Json.JsonConvert.SerializeObject(relationAccountModel);
            bool   result = await database.StringSetAsync(key, value, expiry);

            return(result);
        }
Esempio n. 3
0
        public async Task <RelationAccountModel> GetRelationAccountModel(string relationtoken)
        {
            Enforce.ArgumentNotNull <string>(relationtoken, "Relation Token 不能为null");

            RelationAccountModel relationAccountModel = null;

            string key = string.Concat(prefix, relationtoken);

            string model = await this.database.StringGetAsync(key);

            //await this.database.KeyDeleteAsync(key);

            if (!string.IsNullOrEmpty(model))
            {
                relationAccountModel = Newtonsoft.Json.JsonConvert.DeserializeObject <RelationAccountModel>(model);
            }

            return(relationAccountModel);
        }
Esempio n. 4
0
        public async Task <UserRepoModel> RegisterUser(string submitToken, string redisId, string mail, string password, string name, string agent)
        {
            UserRepoModel userRepoModel = null;

            if (await this.checkSubmitToken(EmailAuthCodeType.RegisterUser, mail, submitToken, agent))
            {
                var thirdParty = new RelationAccountModel();
                if (!string.IsNullOrEmpty(redisId))
                {
                    thirdParty = await _relationRepo.GetRelationAccountModel(redisId);

                    if (null == thirdParty)
                    {
                        Enforce.Throw(new FrontInputValidateErrorException("第三方Id不存在"));
                    }
                }

                var userInfoModel = new UserInfoModel();
                userInfoModel.userName      = name;
                userInfoModel.userTrueName  = name;
                userInfoModel.password      = Encrypt.Base64Encode(password);
                userInfoModel.userLevel     = 0;
                userInfoModel.sex           = 0;
                userInfoModel.userStatus    = 1;
                userInfoModel.lastLoginTime = DateTime.Now;
                bool ret        = false;
                var  userEntity = this.getUserInfoModelByMail(mail, null);
                //是否存在记录(当被邀请时会生成一条占位的记录)
                if (userEntity == null)
                {
                    userInfoModel.mail       = mail.ToLower();;
                    userInfoModel.createTime = userInfoModel.lastLoginTime;
                    if (string.IsNullOrEmpty(name))
                    {
                        userInfoModel.userName     = mail.Split('@')[0];
                        userInfoModel.userTrueName = mail.Split('@')[0];
                    }
                    var user = Mapper.Map <UserInfoModel, T_USER>(userInfoModel);
                    if (string.IsNullOrEmpty(thirdParty.thirdPartyID))
                    {
                        ret = this._userBll.Add(user);
                    }
                    else
                    {
                        if (this.getThirdPartyInfoModel(thirdParty.thirdPartyID, thirdParty.from) != null)
                        {
                            Enforce.Throw(new LogicErrorException("第三方账号已被其他账号绑定"));
                        }
                        ret = this._userBll.AddUserAndThirdParty(user, thirdParty.thirdPartyID, thirdParty.from);
                    }
                    userInfoModel = Mapper.Map <T_USER, UserInfoModel>(user);
                }
                else if (userEntity != null && userEntity.userStatus == 0)
                {
                    userInfoModel.userID     = userEntity.userID;
                    userInfoModel.mail       = userEntity.mail;
                    userInfoModel.createTime = userEntity.createTime;
                    if (string.IsNullOrEmpty(name))
                    {
                        userInfoModel.userName     = userEntity.userName;
                        userInfoModel.userTrueName = userEntity.userTrueName;
                    }
                    if (string.IsNullOrEmpty(thirdParty.thirdPartyID))
                    {
                        ret = this._userBll.Update(Mapper.Map <UserInfoModel, T_USER>(userInfoModel));
                    }
                    else
                    {
                        if (this.getThirdPartyInfoModel(thirdParty.thirdPartyID, thirdParty.from) != null)
                        {
                            Enforce.Throw(new LogicErrorException("第三方账号已被其他账号绑定"));
                        }
                        ret = this._userBll.UpdateUserAndThirdParty(Mapper.Map <UserInfoModel, T_USER>(userInfoModel), thirdParty.thirdPartyID, thirdParty.from);
                    }
                }


                if (ret)
                {
                    userRepoModel          = new UserRepoModel();
                    userRepoModel.info     = userInfoModel;
                    userRepoModel.roleList = this.getUserRoleListByUserID(userRepoModel.info.userID);
                }
            }

            return(userRepoModel);
        }