使用OIDC在身份服务器4中验证WEB API 2.net框架4.x



我知道也有类似的问题,但仍然不太清楚,在阅读了一堆与该主题相关的帖子后,这就是我"理解"代码的样子,我仍然在处理oauth/openid/owin/katana/identityserver等中涉及的所有概念

总体情况是:我有一个角度应用程序,在用户注册和登录的情况下,不需要征得同意,一旦用户登录,SPA将开始与后面的所有api通信,并且api应该能够针对auth服务器进行身份验证。

因此,基本上,我需要我的web api能够在身份服务器4中通过客户端凭据授予类型进行身份验证,并使用身份验证服务器颁发的令牌。

我在identity-server4:中定义了这个客户端(webapi2.netframework4.5(

public static IEnumerable<Client> GetClients()
{    
//client credentials client
return new List<Client>
{
new Client
{  ClientId = "client2",
AllowedGrantTypes = GrantTypes.ClientCredentials,
ClientSecrets =
{
new Secret("secret".Sha256())
},
AllowedScopes = { "api2" }
},
}

在.net Api方面我有这样的:

public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = 
CookieAuthenticationDefaults.AuthenticationType
});
app.UseOpenIdConnectAuthentication(new 
OpenIdConnectAuthenticationOptions
{
ClientId = "client2", 
Authority = "http://localhost:5000",
RequireHttpsMetadata = false,
ResponseType = "id_token",
Scope = "api2",
SignInAsAuthenticationType = 
CookieAuthenticationDefaults.AuthenticationType,
}
}); 

控制器使用Autorize装饰器进行装饰。这些是使用的软件包版本

id="Microsoft.Owin.Security.OpenIdConnect" version="4.0.0"
id="Microsoft.Owin.Security.OAuth" version="4.0.0"
id="Microsoft.Owin.Security" version="4.0.0"
id="Microsoft.Owin" version="4.0.0"

到目前为止,我正在使用官方项目网站上的一个演示项目(https://github.com/IdentityServer/IdentityServer4.Samples),我在MVC演示应用程序中添加了一个额外的调用来调用我的api。

public async Task<IActionResult> CallApiUsingUserAccessToken2()
{
var accessToken = await HttpContext.GetTokenAsync("access_token");
var client = new HttpClient();
client.SetBearerToken(accessToken);
var content = await 
client.GetStringAsync("http://localhost:17307/api
/Organization/GetOrganizationById/2007");
ViewBag.Json = JArray.Parse(content).ToString();
return View("Json");
}

根据工作演示,有两种方法可以做到这一点,但没有一种对我有效

public async Task<IActionResult> CallApiUsingClientCredentials2()
{
var tokenClient = new TokenClient("http://localhost:5000/connect/token", "mvc", "secret");
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api1");
var client = new HttpClient();
client.SetBearerToken(tokenResponse.AccessToken);
var content = await client.GetStringAsync("http://localhost:17307/api/Organization/GetOrganizationById/2007");
ViewBag.Json = JArray.Parse(content).ToString();
return View("Json");
}

这是错误响应的一部分,我在两种情况下都得到了:

<div class="row">
<div class="col-sm-6">
<div class="alert alert-danger">
Sorry, there was an error
<strong>
<em>
: invalid_request
</em>
</strong>
<div>Invalid redirect_uri</div>
</div>
<div class="request-id">Request Id: 0HLIALF7L4N8J:00000001</div>
</div>
</div>

这里缺少什么或出了什么问题,redirect_uri是强制性的吗?为什么没有出现在.net核心的配置部分中?

这就是api的配置在.net核心中的样子,并且运行良好。

public void ConfigureServices(IServiceCollection services)
{    
services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters();
services.AddAuthentication("Bearer")
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.ApiName = "api1";
});
}

提前谢谢。

更新

经过一些实验,我确认我遇到的问题是在使用owin中间件验证访问令牌的api中。

public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies",
});
JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, 
string> 
();
app.UseIdentityServerBearerTokenAuthentication
(new IdentityServerBearerTokenAuthenticationOptions
{
Authority = "http://localhost:5000",
RequiredScopes = new[] { "api2" },
});
}

我正在使用identityserver3.accesstokenvalidation来执行验证,这是建议的,但在客户端应用程序中获得访问令牌并将其传递给api请求后,我收到了一个401未经授权的错误,这是因为它希望在安全HTTPS下操作吗?,我注意到对于accesstokenvalidation v4,你可以设置"RequireHttpsMetadata=false",但我在v3中没有看到这一点,这可能是我没有得到令牌验证的原因吗?

尝试在从"mvc"到"client2">的这一行中首先使用正确的client_id

var tokenClient=新的tokenClient("http://localhost:5000/connect/token","mvc">,"secret"(;

最新更新