Пример #1
0
        public async Task <IActionResult> SignInBindCallBack(string provider, string redirectUrl = "", string token = "")
        {
            if (string.IsNullOrWhiteSpace(provider))
            {
                return(BadRequest());
            }

            if (!await HttpContext.IsProviderSupportedAsync(provider))
            {
                return(BadRequest());
            }

            if (token.IsNullOrEmpty() || !token.StartsWith("Bearer "))
            {
                return(Redirect($"{redirectUrl}#bind-result?code={ErrorCode.Fail}&message={HttpUtility.UrlEncode("请先登录")}"));
            }
            else
            {
                token = token.Remove(0, 7);
            }

            AuthenticateResult authenticateResult = await _contextAccessor.HttpContext.AuthenticateAsync(provider);

            if (!authenticateResult.Succeeded)
            {
                return(Redirect($"{redirectUrl}#bind-result?code=fail&message={authenticateResult.Failure.Message}"));
            }
            var openIdClaim = authenticateResult.Principal.FindFirst(ClaimTypes.NameIdentifier);

            if (openIdClaim == null || string.IsNullOrWhiteSpace(openIdClaim.Value))
            {
                return(Redirect($"{redirectUrl}#bind-result?code={ErrorCode.Fail}&message={HttpUtility.UrlEncode("未能获取openId")}"));
            }

            JwtPayload jwtPayload     = (JwtPayload)_jsonWebTokenService.Decode(token);
            string     nameIdentifier = jwtPayload.Claims.FirstOrDefault(r => r.Type == ClaimTypes.NameIdentifier)?.Value;

            if (nameIdentifier.IsNullOrWhiteSpace())
            {
                return(Redirect($"{redirectUrl}#bind-result?code={ErrorCode.Fail}&message={HttpUtility.UrlEncode("请先登录")}"));
            }
            long             userId = long.Parse(nameIdentifier);
            UnifyResponseDto unifyResponseDto;

            switch (provider)
            {
            case LinUserIdentity.GitHub:
                unifyResponseDto = await _userCommunityService.BindGitHubAsync(authenticateResult.Principal, openIdClaim.Value, userId);

                break;

            case LinUserIdentity.QQ:
                unifyResponseDto = await _userCommunityService.BindQQAsync(authenticateResult.Principal, openIdClaim.Value, userId);

                break;

            case LinUserIdentity.Gitee:
                unifyResponseDto = await _userCommunityService.BindGiteeAsync(authenticateResult.Principal, openIdClaim.Value, userId);

                break;
            //case LinUserIdentity.WeiXin:

            //    break;
            default:
                _logger.LogError($"未知的privoder:{provider},redirectUrl:{redirectUrl}");
                unifyResponseDto = UnifyResponseDto.Error($"未知的privoder:{provider}!");
                break;
            }

            return(Redirect($"{redirectUrl}#bind-result?code={unifyResponseDto.Code.ToString()}&message={HttpUtility.UrlEncode(unifyResponseDto.Message.ToString())}"));
        }