C# 如何使用Google.api.Auth.OAuth2.UserCredential中的令牌信息在发送到webapi端点的请求中构建cookie
在WPF应用程序中,用户使用以下代码与Google进行身份验证:C# 如何使用Google.api.Auth.OAuth2.UserCredential中的令牌信息在发送到webapi端点的请求中构建cookie,c#,.net,authentication,google-api,openid,C#,.net,Authentication,Google Api,Openid,在WPF应用程序中,用户使用以下代码与Google进行身份验证: using Google.Apis.Auth.OAuth2; ... public void Authenticate() { UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync( new ClientSecrets { ClientId = "myClientId",
using Google.Apis.Auth.OAuth2;
...
public void Authenticate()
{
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "myClientId",
ClientSecret = "myClientSecret"
},
new[] { "email", "openid" },
"user",
CancellationToken.None).Result;
}
这起作用,并且UserCredential
对象包含经过身份验证的令牌:
为了调用webapi端点,我想将此令牌信息嵌入使用HttpClient
发出的web请求中,这是否可能,以及如何实现
我认为请求必须使用一些cookie来通知服务器它已通过身份验证,但我不知道确切的是哪些cookie
服务器端的端点在IdentityServer的帮助下验证用户是否已通过身份验证:
var result = await HttpContext.AuthenticateAsync(IdentityServer4.IdentityServerConstants.ExternalCookieAuthenticationScheme);
if (result?.Succeeded != true)
{
throw new Exception("External authentication error");
}
您希望将其用于什么api端点?技术上,您只需要刷新令牌,然后可以在需要时请求新的访问令牌。任何google api端点都只需要访问令牌就可以使请求有效,并感谢您的回复。我正在访问的api端点是来自我编码的web服务器后端的端点。该api端点使用IdentityServer和上面的代码段验证请求是否通过了Google外部提供者的身份验证。不幸的是,我不知道
authenticatesync
检查了什么,我想设置了标准的身份验证cookie。这就是为什么我想将从Google获得的令牌信息嵌入调用我的web api的请求中。