如何在移动应用程序上进行身份validation后在.NET Core服务器上登录用户

我遇到了.NET Core Web API应用程序身份validation的麻烦。 我想:1)在移动应用程序(目前为iOS)上使用Google对用户进行身份validation2)使用此身份validation,使用AspNetCore.Identity和Entity Framework Core在数据库中创建用户记录3)使用相同的身份validation,从.NET Core调用Google Calendar API服务器

到目前为止,我想出了如何实现1和3,但无法绕过2号。

我的理解是,要登录用第三方认证的用户,由于文档的原因,您需要使用SignInManager实例方法ExternalLoginSignInAsync 。 它需要两个参数: 登录提供程序 (应该简单地称为“Google”)和唯一的提供程序密钥 。 我的问题是我找不到任何地方可以找到一个。

以下是我在移动应用上通过Google登录结果收到的所有内容列表: 在此处输入图像描述

这是我尝试调用的方法。

// POST api/signup [HttpPost] public async Task Post([FromBody]string authorizationCode, [FromBody]string userId) { var tokenFromAuthorizationCode = await GetGoogleTokens(userId, authorizationCode); var result = await signInManager.ExternalLoginSignInAsync( "Google", tokenFromAuthorizationCode.IdToken, false); if (result.Succeeded) return true; var externalLoginInfo = new ExternalLoginInfo( ClaimsPrincipal.Current, "Google", tokenFromAuthorizationCode.IdToken, null); return await SignInUser(externalLoginInfo); } private async Task SignInUser(ExternalLoginInfo info) { var newUser = new AppUser { Email = "test@test.com", UserName = "TestUser" }; var identResult = await userManager.CreateAsync(newUser); if (identResult.Succeeded) { identResult = await userManager.AddLoginAsync(newUser, info); if (identResult.Succeeded) { await signInManager.SignInAsync(newUser, false); return true; } } return false; } private async Task GetGoogleTokens(string userId, string authorizationCode) { TokenResponse token; try { // TODO: Save access and refresh token to AppUser object token = await authFlow.Flow.ExchangeCodeForTokenAsync( userId, authorizationCode, "http://localhost:60473/signin-google", CancellationToken.None); } catch (Exception e) { Console.WriteLine(e); throw; } return token; } 

我的问题是:如果您通过REST API构建身份validation,这是一条正确的路径,如果是这样,我在哪里可以获得Google的提供商密钥 ? 提前致谢。

很明显, 提供商密钥只是来自Google的用户ID 。 这是适合我的解决方案:

  [HttpPost] public async Task Post([FromBody]GoogleSignInCredentials credentials) { // 1. get user id from idToken var oauthService = new Oauth2Service(new BaseClientService.Initializer { ApiKey = "{your api key}" }); var tokenInfoRequest = oauthService.Tokeninfo(); tokenInfoRequest.IdToken = credentials.IdToken; var userInfo = await tokenInfoRequest.ExecuteAsync(); // 2. get access_token and refresh_token with new id and authorization code var tokenFromAuthorizationCode = await GetGoogleTokens(userInfo.UserId, credentials.AuthorizationCode); // 3. check if user exists var result = await _signInManager.ExternalLoginSignInAsync( "Google", userInfo.UserId, false); if (result.Succeeded) return await _userManager.FindByEmailAsync(userInfo.Email); // 4. create user account var externalLoginInfo = new ExternalLoginInfo( ClaimsPrincipal.Current, "Google", userInfo.UserId, null); // 5. fetch user var createdUser = await SignInUser(externalLoginInfo, userInfo.Email); if (createdUser != null) { createdUser.GoogleAccessToken = tokenFromAuthorizationCode.AccessToken; createdUser.GoogleRefreshToken = tokenFromAuthorizationCode.RefreshToken; var updateResult = await _userManager.UpdateAsync(createdUser); if (updateResult.Succeeded) return createdUser; return null; } return null; } private async Task SignInUser(ExternalLoginInfo info, string email) { var newUser = new AppUser { Email = email, UserName = email }; var identResult = await _userManager.CreateAsync(newUser); if (identResult.Succeeded) { identResult = await _userManager.AddLoginAsync(newUser, info); if (identResult.Succeeded) { await _signInManager.SignInAsync(newUser, false); return await _userManager.FindByEmailAsync(email); } } return null; } private async Task GetGoogleTokens(string userId, string authorizationCode) { return await _authFlow.Flow.ExchangeCodeForTokenAsync( userId, authorizationCode, "http://localhost:60473/signin-google", CancellationToken.None); }