1. 程式人生 > >AbpZero之企業微信---登錄(拓展第三方auth授權登錄)---第三步:需要註意事項

AbpZero之企業微信---登錄(拓展第三方auth授權登錄)---第三步:需要註意事項

login eat exce sync private open 生成 isp lose

1、AbpZero的auth登錄會在數據庫中的AbpUserLogins表會根據你登錄的ProviderKey和Provider來生成生成一條記錄,ProviderKey在表中是唯一的;

2、要登錄成功還得重寫下TokenAuthController.cs下的GetExternalUserInfo方法,代碼如下

  private async Task<ExternalAuthUserInfo> GetExternalUserInfo(ExternalAuthenticateModel model)
        {
            var userInfo = await
_externalAuthManager.GetUserInfo(model.AuthProvider, model.ProviderAccessCode); //if (userInfo.ProviderKey != model.ProviderKey) //{ // throw new UserFriendlyException(L("CouldNotValidateExternalUser")); //} return userInfo; }

3、由於我公司需要集成企業微信,小程序,釘釘等登錄,但用戶的唯一值都是一樣的,所以我們也重寫了ExternalAuthenticate方法,代碼如下

技術分享圖片
   [HttpPost]
        public async Task<ExternalAuthenticateResultModel> WeChatAuthenticate([FromBody] ExternalAuthenticateModel model)
        {
            var externalUser = await GetExternalUserInfo(model);

            //Logger.Debug(Newtonsoft.Json.JsonConvert.SerializeObject(new UserLoginInfo(model.AuthProvider, externalUser.ProviderKey, model.AuthProvider) ) + GetTenancyNameOrNull());
var loginResult = await _logInManager.LoginAsync(new UserLoginInfo(model.AuthProvider, externalUser.ProviderKey, model.AuthProvider), GetTenancyNameOrNull()); //Logger.Debug(loginResult.Result.ToString()); switch (loginResult.Result) { case AbpLoginResultType.Success: { var accessToken = CreateAccessToken(CreateJwtClaims(loginResult.Identity)); var returnUrl = model.ReturnUrl; if (model.SingleSignIn.HasValue && model.SingleSignIn.Value && loginResult.Result == AbpLoginResultType.Success) { loginResult.User.SetSignInToken(); returnUrl = AddSingleSignInParametersToReturnUrl(model.ReturnUrl, loginResult.User.SignInToken, loginResult.User.Id, loginResult.User.TenantId); } return new ExternalAuthenticateResultModel { AccessToken = accessToken, EncryptedAccessToken = GetEncrpyedAccessToken(accessToken), ExpireInSeconds = (int)_configuration.Expiration.TotalSeconds, ReturnUrl = returnUrl }; } case AbpLoginResultType.UnknownExternalLogin: { var newUser = await RegisterExternalUserAsync(externalUser); if (!newUser.IsActive) { return new ExternalAuthenticateResultModel { WaitingForActivation = true }; } //Try to login again with newly registered user! loginResult = await _logInManager.LoginAsync(new UserLoginInfo(model.AuthProvider, externalUser.ProviderKey, model.AuthProvider), GetTenancyNameOrNull()); if (loginResult.Result != AbpLoginResultType.Success) { throw _abpLoginResultTypeHelper.CreateExceptionForFailedLoginAttempt( loginResult.Result, model.ProviderKey, GetTenancyNameOrNull() ); } var accessToken = CreateAccessToken(CreateJwtClaims(loginResult.Identity)); return new ExternalAuthenticateResultModel { AccessToken = accessToken, EncryptedAccessToken = GetEncrpyedAccessToken(accessToken), ExpireInSeconds = (int)_configuration.Expiration.TotalSeconds }; } default: { throw _abpLoginResultTypeHelper.CreateExceptionForFailedLoginAttempt( loginResult.Result, model.ProviderKey, GetTenancyNameOrNull() ); } } }
View Code

AbpZero之企業微信---登錄(拓展第三方auth授權登錄)---第三步:需要註意事項