感谢园友 @turingguo 发布的 https://www.cnblogs.com/turingguo/p/9019026.html 文章,详细介绍了ABP Zero集成微信小程序登陆的实现过程。
经测试,abp的外部登陆有几处bug,在此做记录。
1、xxxxWebHostModule.cs文件
1 public override void PostInitialize() 2 { 3 4 if (!IocManager.Resolve<IMultiTenancyConfig>().IsEnabled) 5 { 6 return; 7 } 8 9 if (!DatabaseCheckHelper.Exist(_appConfiguration["ConnectionStrings:Default"])) 10 { 11 return; 12 } 13 14 var workManager = IocManager.Resolve<IBackgroundWorkerManager>(); 15 workManager.Add(IocManager.Resolve<SubscriptionExpirationCheckWorker>()); 16 workManager.Add(IocManager.Resolve<SubscriptionExpireEmailNotifierWorker>()); 17 18 ConfigureExternalAuthProviders(); 19 }
当启用多租户模式的情况下,租主站点没有将外部登陆的provider添加到集合中。
不未启用多租户模式时,第四行的逻辑判断又给跳过了。
因此将18行代码调整到第3行。
2、TokenAuthController文件
1 public async Task<ExternalAuthenticateResultModel> ExternalAuthenticate([FromBody] ExternalAuthenticateModel model) 2 { 3 var externalUser = await GetExternalUserInfo(model); 4 5 var loginResult = await _logInManager.LoginAsync(new UserLoginInfo(model.AuthProvider, externalUser.ProviderKey, model.AuthProvider), GetTenancyNameOrNull()); 6 7 switch (loginResult.Result) 8 { 9 case AbpLoginResultType.Success: 10 { 11 var accessToken = CreateAccessToken(CreateJwtClaims(loginResult.Identity)); 12 13 var returnUrl = model.ReturnUrl; 14 15 if (model.SingleSignIn.HasValue && model.SingleSignIn.Value && loginResult.Result == AbpLoginResultType.Success) 16 { 17 loginResult.User.SetSignInToken(); 18 returnUrl = AddSingleSignInParametersToReturnUrl(model.ReturnUrl, loginResult.User.SignInToken, loginResult.User.Id, loginResult.User.TenantId); 19 } 20 21 return new ExternalAuthenticateResultModel 22 { 23 AccessToken = accessToken, 24 EncryptedAccessToken = GetEncrpyedAccessToken(accessToken), 25 ExpireInSeconds = (int)_configuration.Expiration.TotalSeconds, 26 ReturnUrl = returnUrl 27 }; 28 } 29 case AbpLoginResultType.UnknownExternalLogin: 30 { 31 var newUser = await RegisterExternalUserAsync(externalUser); 32 if (!newUser.IsActive) 33 { 34 return new ExternalAuthenticateResultModel 35 { 36 WaitingForActivation = true 37 }; 38 } 39 40 //Try to login again with newly registered user! 41 loginResult = await _logInManager.LoginAsync(new UserLoginInfo(model.AuthProvider, externalUser.ProviderKey, model.AuthProvider), GetTenancyNameOrNull()); 42 if (loginResult.Result != AbpLoginResultType.Success) 43 { 44 throw _abpLoginResultTypeHelper.CreateExceptionForFailedLoginAttempt( 45 loginResult.Result, 46 model.ProviderKey, 47 GetTenancyNameOrNull() 48 ); 49 } 50 51 var accessToken = CreateAccessToken(CreateJwtClaims(loginResult.Identity)); 52 return new ExternalAuthenticateResultModel 53 { 54 AccessToken = accessToken, 55 EncryptedAccessToken = GetEncrpyedAccessToken(accessToken), 56 ExpireInSeconds = (int)_configuration.Expiration.TotalSeconds 57 }; 58 } 59 default: 60 { 61 throw _abpLoginResultTypeHelper.CreateExceptionForFailedLoginAttempt( 62 loginResult.Result, 63 model.ProviderKey, 64 GetTenancyNameOrNull() 65 ); 66 } 67 } 68 }
分别在第5行和41行,是用户外部登陆的过程。该两行代码的BUG是,登陆时使用了微信的accessCode当作providerKey登陆,方法是将 await GetExternalUserInfo(model) 返回的ProviderKey做为用户登陆就可以了。(以上代码是修改后的代码)