问题描述
我的起点是 OpenIddict samples 中的 Velusia 示例,并且我已成功登录。我现在正在试验外部 Web API,方法是将 Visual Studio API 项目添加到返回伪天气预报 (Weather API) 的解决方案中。
当我运行解决方案并登录时,可以通过服务器内的 ResourceController 调用测试消息而不会出错。
由于我不希望将 API 访问令牌存储在浏览器中,因为我认为这是不好的做法,因此我的计划是通过向服务器的上述 添加一个方法来调用 Weather API em>ResourceController 对 API 进行 HTTP 调用。问题是我得到了一个无效的令牌,所以显然我没有正确生成它。
这是我添加到 ResourceController 中的方法的代码。
[Authorize(AuthenticationSchemes = OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme)]
[HttpGet("weather-forecast")]
public async Task<IActionResult> WeatherForecast()
{
var token = await HttpContext.GetTokenAsync(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme,OpenIdConnectParameterNames.Accesstoken);
if (string.IsNullOrEmpty(token))
{
throw new InvalidOperationException("Truncated error message...");
}
using var request = new HttpRequestMessage(HttpMethod.Get,"https://localhost:5003/weatherforecast");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer",token);
using var response = await _httpClient.SendAsync(request);
...
OpenIddict 的服务器配置为:
services.AddOpenIddict()
.AddCore(options =>
{
options.UseEntityFrameworkCore()
.UseDbContext<ApplicationDbContext>();
})
.AddServer(options =>
{
options
.SetAuthorizationEndpointUris("/connect/authorize")
.SetlogoutEndpointUris("/connect/logout")
.SetTokenEndpointUris("/connect/token")
.SetUserinfoEndpointUris("/connect/userinfo")
.SetVerificationEndpointUris("/connect/verify")
;
options.RegisterScopes(Scopes.Email,Scopes.Profile,Scopes.Roles);
options.AllowAuthorizationCodeFlow();
options.AllowClientCredentialsFlow();
options.AllowRefreshTokenFlow();
options.AddDevelopmentEncryptionCertificate().AddDevelopmentSigningCertificate();
options.AddEphemeralEncryptionKey().AddEphemeralSigningKey().disableAccesstokenEncryption();
options.UseAspNetCore()
.EnableStatusCodePagesIntegration()
.EnableAuthorizationEndpointPassthrough()
.EnablelogoutEndpointPassthrough()
.EnabletokenEndpointPassthrough()
.EnableuserinfoEndpointPassthrough()
.EnableStatusCodePagesIntegration()
.EnabLeverificationEndpointPassthrough()
;
})
.AddValidation(options =>
{
options.UseLocalServer();
options.UseAspNetCore();
});
Weather API 启动包括:
services.AddAuthentication(options =>
{
options.DefaultScheme = OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme;
});
// Register the OpenIddict validation components.
services.AddOpenIddict()
.AddValidation(options =>
{
options.SetIssuer(Common.Urls.stuVelusiaServer);
options.SetClientId("testapi");
options.SetClientSecret("secret");
options.UseSystemNetHttp();
options.UseAspNetCore();
options.UseLocalServer();
});
并且服务器项目中的 Weather API 描述符是:
var descriptor = new OpenIddictApplicationDescriptor
{
ClientId = "testapi",ClientSecret = "secret",ConsentType = OpenIddictConstants.ConsentTypes.Implicit,displayName = "Test API",Permissions =
{
OpenIddictConstants.Permissions.GrantTypes.AuthorizationCode,OpenIddictConstants.Permissions.Endpoints.Authorization,OpenIddictConstants.Permissions.Endpoints.Introspection,OpenIddictConstants.Permissions.Endpoints.Token,OpenIddictConstants.Permissions.GrantTypes.RefreshToken,//OpenIddictConstants.Permissions.GrantTypes.ClientCredentials,},Requirements =
{
OpenIddictConstants.Requirements.Features.ProofKeyForCodeExchange
}
};
如何为机器对机器调用创建正确的令牌?在生产项目中,我需要知道登录用户的身份(至少是他们的主题 或电子邮件地址)。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)