コード例 #1
0
        /// <summary>
        /// 记录授权成功后的信息
        /// </summary>
        /// <param name="principal"></param>
        /// <param name="openId"></param>
        /// <returns></returns>
        public async Task <long> SaveGitHubAsync(ClaimsPrincipal principal, string openId)
        {
            string email        = principal.FindFirst(ClaimTypes.Email)?.Value;
            string name         = principal.FindFirst(ClaimTypes.Name)?.Value;
            string gitHubName   = principal.FindFirst(GitHubAuthenticationConstants.Claims.Name)?.Value;
            string gitHubApiUrl = principal.FindFirst(GitHubAuthenticationConstants.Claims.Url)?.Value;
            string avatarUrl    = principal.FindFirst(LinConsts.Claims.AvatarUrl)?.Value;
            string bio          = principal.FindFirst(LinConsts.Claims.BIO)?.Value;
            string blogAddress  = principal.FindFirst(LinConsts.Claims.BlogAddress)?.Value;
            Expression <Func <LinUserIdentity, bool> > expression = r =>
                                                                    r.IdentityType == LinUserIdentity.GitHub && r.Credential == openId;

            LinUserIdentity linUserIdentity = await _freeSql.Select <LinUserIdentity>().Where(expression).FirstAsync();

            long userId = 0;

            if (linUserIdentity == null)
            {
                LinUser user = new LinUser
                {
                    Active        = (int)UserActive.Active,
                    Avatar        = avatarUrl,
                    CreateTime    = DateTime.Now,
                    LastLoginTime = DateTime.Now,
                    Email         = email,
                    Introduction  = bio,
                    LinUserGroups = new List <LinUserGroup>()
                    {
                        new LinUserGroup()
                        {
                            GroupId = LinConsts.Group.User
                        }
                    },
                    Nickname         = gitHubName,
                    Username         = "",
                    BlogAddress      = blogAddress,
                    LinUserIdentitys = new List <LinUserIdentity>()
                    {
                        new LinUserIdentity
                        {
                            CreateTime   = DateTime.Now,
                            Credential   = openId,
                            IdentityType = LinUserIdentity.GitHub,
                            Identifier   = name,
                        }
                    }
                };
                await _userRepository.InsertAsync(user);

                _userRepository.UnitOfWork.Commit();
                userId = user.Id;
            }
            else
            {
                userId = linUserIdentity.CreateUserId;
                await _userRepository.UpdateLastLoginTimeAsync(linUserIdentity.CreateUserId);
            }

            return(userId);
        }
コード例 #2
0
        /// <summary>
        /// qq快速登录的信息,唯一值openid,昵称(nickname),性别(gender),picture(30像素),picture_medium(50像素),picture_full 100 像素,avatar(40像素),avatar_full(100像素)
        /// </summary>
        /// <param name="principal"></param>
        /// <param name="openId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task <long> SaveQQAsync(ClaimsPrincipal principal, string openId)
        {
            string nickname       = principal.FindFirst(ClaimTypes.Name)?.Value;
            string gender         = principal.FindFirst(ClaimTypes.Gender)?.Value;
            string picture        = principal.FindFirst(QQAuthenticationConstants.Claims.PictureUrl)?.Value;
            string picture_medium = principal.FindFirst(QQAuthenticationConstants.Claims.PictureMediumUrl)?.Value;
            string picture_full   = principal.FindFirst(QQAuthenticationConstants.Claims.PictureFullUrl)?.Value;
            string avatar         = principal.FindFirst(QQAuthenticationConstants.Claims.AvatarUrl)?.Value;
            string avatar_full    = principal.FindFirst(QQAuthenticationConstants.Claims.AvatarFullUrl)?.Value;

            Expression <Func <LinUserIdentity, bool> > expression = r =>
                                                                    r.IdentityType == LinUserIdentity.QQ && r.Credential == openId;

            LinUserIdentity linUserIdentity = await _userIdentityRepository.Where(expression).FirstAsync();

            long userId = 0;

            if (linUserIdentity == null)
            {
                LinUser user = new LinUser
                {
                    Active        = (int)UserActive.Active,
                    Avatar        = avatar_full,
                    CreateTime    = DateTime.Now,
                    LastLoginTime = DateTime.Now,
                    Email         = "",
                    Introduction  = "",
                    LinUserGroups = new List <LinUserGroup>()
                    {
                        new LinUserGroup()
                        {
                            GroupId = LinConsts.Group.User
                        }
                    },
                    Nickname         = nickname,
                    Username         = "",
                    BlogAddress      = "",
                    LinUserIdentitys = new List <LinUserIdentity>()
                    {
                        new LinUserIdentity
                        {
                            CreateTime   = DateTime.Now,
                            Credential   = openId,
                            IdentityType = LinUserIdentity.GitHub,
                            Identifier   = nickname,
                        }
                    }
                };
                await _userRepository.InsertAsync(user);

                userId = user.Id;
            }
            else
            {
                userId = linUserIdentity.CreateUserId;
                await _userRepository.UpdateLastLoginTimeAsync(linUserIdentity.CreateUserId);
            }

            return(userId);
        }
コード例 #3
0
        public bool VerifyUsernamePassword(long userId, string username, string password)
        {
            LinUserIdentity userIdentity = _freeSql.Select <LinUserIdentity>()
                                           .Where(r => r.CreateUserId == userId && r.Identifier == username)
                                           .ToOne();

            return(userIdentity != null && EncryptUtil.Verify(userIdentity.Credential, password));
        }
コード例 #4
0
        public async Task <bool> VerifyUserPasswordAsync(long userId, string password)
        {
            LinUserIdentity userIdentity = await _userIdentityRepository
                                           .Where(r => r.CreateUserId == userId && r.IdentityType == LinUserIdentity.Password)
                                           .ToOneAsync();

            return(userIdentity != null && EncryptUtil.Verify(userIdentity.Credential, password));
        }
コード例 #5
0
        public async Task <bool> VerifyUserPasswordAsync(long userId, string password, string salt)
        {
            LinUserIdentity userIdentity = await this.GetFirstByUserIdAsync(userId);

            string encryptPassword = _cryptographyService.Encrypt(password, salt);

            return(userIdentity != null && userIdentity.Credential == encryptPassword);
        }
コード例 #6
0
        public async Task <long> SaveGiteeAsync(ClaimsPrincipal principal, string openId)
        {
            LinUserIdentity linUserIdentity = await _userIdentityRepository.Where(r => r.IdentityType == LinUserIdentity.Gitee && r.Credential == openId).FirstAsync();

            long userId = 0;

            if (linUserIdentity == null)
            {
                string email = principal.FindFirst(ClaimTypes.Email)?.Value;
                string name  = principal.FindFirst(ClaimTypes.Name)?.Value;

                //string giteeUrl = principal.FindFirst(GiteeAuthenticationConstants.Claims.Url)?.Value;
                string nickname = principal.FindFirst(GiteeAuthenticationConstants.Claims.Name)?.Value;

                string avatarUrl   = principal.FindFirst("urn:gitee:avatar_url")?.Value;
                string blogAddress = principal.FindFirst("urn:gitee:blog")?.Value;
                string bio         = principal.FindFirst("urn:gitee:bio")?.Value;
                string htmlUrl     = principal.FindFirst("urn:gitee:html_url")?.Value;

                LinUser user = new LinUser
                {
                    Active        = UserActive.Active,
                    Avatar        = avatarUrl,
                    LastLoginTime = DateTime.Now,
                    Email         = email,
                    Introduction  = bio + htmlUrl,
                    LinUserGroups = new List <LinUserGroup>()
                    {
                        new LinUserGroup()
                        {
                            GroupId = LinConsts.Group.User
                        }
                    },
                    Nickname         = nickname,
                    Username         = "",
                    BlogAddress      = blogAddress,
                    LinUserIdentitys = new List <LinUserIdentity>()
                    {
                        new LinUserIdentity(LinUserIdentity.Gitee, name, openId, DateTime.Now)
                    }
                };
                await _userRepository.InsertAsync(user);

                userId = user.Id;
            }
            else
            {
                userId = linUserIdentity.CreateUserId;
            }

            return(userId);
        }
コード例 #7
0
        /// <summary>
        /// qq快速登录的信息,唯一值openid,昵称(nickname),性别(gender),picture(30像素),picture_medium(50像素),picture_full 100 像素,avatar(40像素),avatar_full(100像素)
        /// </summary>
        /// <param name="principal"></param>
        /// <param name="openId"></param>
        /// <returns></returns>
        public override async Task <long> SaveUserAsync(ClaimsPrincipal principal, string openId)
        {
            LinUserIdentity linUserIdentity = await _userIdentityRepository.Where(r => r.IdentityType == LinUserIdentity.QQ && r.Credential == openId).FirstAsync();

            long userId = 0;

            if (linUserIdentity == null)
            {
                string nickname       = principal.FindFirst(ClaimTypes.Name)?.Value ?? "默认昵称";
                string gender         = principal.FindFirst(ClaimTypes.Gender)?.Value;
                string picture        = principal.FindFirst(QQAuthenticationConstants.Claims.PictureUrl)?.Value;
                string picture_medium = principal.FindFirst(QQAuthenticationConstants.Claims.PictureMediumUrl)?.Value;
                string picture_full   = principal.FindFirst(QQAuthenticationConstants.Claims.PictureFullUrl)?.Value;
                string avatarUrl      = principal.FindFirst(QQAuthenticationConstants.Claims.AvatarUrl)?.Value;
                string avatarFullUrl  = principal.FindFirst(QQAuthenticationConstants.Claims.AvatarFullUrl)?.Value;

                LinUser user = new LinUser
                {
                    Active        = UserActive.Active,
                    Avatar        = avatarFullUrl,
                    LastLoginTime = DateTime.Now,
                    Email         = "",
                    Introduction  = "",
                    LinUserGroups = new List <LinUserGroup>()
                    {
                        new LinUserGroup()
                        {
                            GroupId = LinConsts.Group.User
                        }
                    },
                    Nickname         = nickname,
                    Username         = "",
                    BlogAddress      = "",
                    LinUserIdentitys = new List <LinUserIdentity>()
                    {
                        new LinUserIdentity(LinUserIdentity.QQ, nickname, openId, DateTime.Now)
                    }
                };
                await _userRepository.InsertAsync(user);

                userId = user.Id;
            }
            else
            {
                userId = linUserIdentity.CreateUserId;
            }

            return(userId);
        }
コード例 #8
0
        public async Task ChangePasswordAsync(LinUserIdentity linUserIdentity, string newpassword)
        {
            string encryptPassword = EncryptUtil.Encrypt(newpassword);

            if (linUserIdentity == null)
            {
                linUserIdentity = new LinUserIdentity(LinUserIdentity.Password, "", encryptPassword, DateTime.Now);
                await _userIdentityRepository.InsertAsync(linUserIdentity);
            }
            else
            {
                linUserIdentity.Credential = encryptPassword;
                await _userIdentityRepository.UpdateAsync(linUserIdentity);
            }
        }
コード例 #9
0
        public Task ChangePasswordAsync(LinUserIdentity linUserIdentity, string newpassword, string salt)
        {
            string encryptPassword = _cryptographyService.Encrypt(newpassword, salt);

            if (linUserIdentity == null)
            {
                linUserIdentity = new LinUserIdentity(LinUserIdentity.Password, "", encryptPassword, DateTime.Now);
                return(_userIdentityRepository.InsertAsync(linUserIdentity));
            }
            else
            {
                linUserIdentity.Credential = encryptPassword;
                return(_userIdentityRepository.UpdateAsync(linUserIdentity));
            }
        }
コード例 #10
0
        public async Task ChangePasswordAsync(ChangePasswordDto passwordDto)
        {
            long currentUserId = _currentUser.Id ?? 0;

            LinUserIdentity userIdentity = await _userIdentityService.GetFirstByUserIdAsync(currentUserId);

            if (userIdentity != null)
            {
                bool valid = EncryptUtil.Verify(userIdentity.Credential, passwordDto.OldPassword);
                if (!valid)
                {
                    throw new LinCmsException("旧密码不正确");
                }
            }

            await _userIdentityService.ChangePasswordAsync(userIdentity, passwordDto.NewPassword);
        }
コード例 #11
0
        private async Task <UnifyResponseDto> BindAsync(string identityType, string name, string openId, long userId)
        {
            LinUserIdentity linUserIdentity = await _userIdentityRepository.Where(r => r.IdentityType == identityType && r.Credential == openId).FirstAsync();

            if (linUserIdentity == null)
            {
                var userIdentity = new LinUserIdentity(identityType, name, openId, DateTime.Now);
                userIdentity.CreateUserId = userId;
                await _userIdentityRepository.InsertAsync(userIdentity);

                return(UnifyResponseDto.Success("绑定成功"));
            }
            else
            {
                return(UnifyResponseDto.Error("绑定失败,该用户已绑定其他账号"));
            }
        }
コード例 #12
0
        public async Task UnBind(Guid id)
        {
            LinUserIdentity userIdentity = await _userIdentityRepository.GetAsync(id);

            if (userIdentity == null || userIdentity.CreateUserId != CurrentUser.Id)
            {
                throw new LinCmsException("你无权解绑此账号");
            }

            List <LinUserIdentity> userIdentities = await _userIdentityRepository.Select.Where(r => r.CreateUserId == CurrentUser.Id).ToListAsync();

            bool hasPwd = userIdentities.Where(r => r.IdentityType == LinUserIdentity.Password).Any();

            if (!hasPwd && userIdentities.Count == 1)
            {
                throw new LinCmsException("你未设置密码,无法解绑最后一个第三方登录账号");
            }
            await _userIdentityRepository.DeleteAsync(userIdentity);
        }
コード例 #13
0
        public async Task ChangePasswordAsync(LinUserIdentity linUserIdentity, string newpassword)
        {
            string encryptPassword = EncryptUtil.Encrypt(newpassword);

            if (linUserIdentity == null)
            {
                linUserIdentity = new LinUserIdentity()
                {
                    IdentityType = LinUserIdentity.Password,
                    Identifier   = "",
                    Credential   = encryptPassword
                };
                await _userIdentityRepository.InsertAsync(linUserIdentity);
            }
            else
            {
                linUserIdentity.Credential = encryptPassword;
                await _userIdentityRepository.UpdateAsync(linUserIdentity);
            }
        }
コード例 #14
0
        public async Task <bool> VerifyUserPasswordAsync(long userId, string password)
        {
            LinUserIdentity userIdentity = await this.GetFirstByUserIdAsync(userId);

            return(userIdentity != null && EncryptUtil.Verify(userIdentity.Credential, password));
        }