AbpZero之企业微信---登录(拓展第三方auth授权登录)---第三步:需要注意事项
Posted 老李。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AbpZero之企业微信---登录(拓展第三方auth授权登录)---第三步:需要注意事项相关的知识,希望对你有一定的参考价值。
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() ); } } }
以上是关于AbpZero之企业微信---登录(拓展第三方auth授权登录)---第三步:需要注意事项的主要内容,如果未能解决你的问题,请参考以下文章
14ABPZero系列教程之拼多多卖家工具 新建微信公众号模块
13ABPZero系列教程之拼多多卖家工具 微信公众号开发前的准备