我知道那里也有类似的问题,但是仍然不是很清楚, 在阅读了与该主题相关的大量帖子之后,这就是我如何“理解”代码的样子,我仍在处理与oauth / openid / owin / katana / identityserver等有关的所有概念。
大图是:我有一个角度应用程序, 在用户注册并登录的地方,无需征得用户的同意,一旦用户登录,SPA就会开始与背面的所有api通信,并且api应该能够通过auth服务器进行身份验证。
因此,基本上,我需要我的Web api能够在身份服务器4中通过客户端凭据授予类型使用身份验证服务器发出的令牌进行身份验证。
我在身份服务器4中定义了此客户端(web api 2 .net framework 4.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";
});
}
谢谢。
更新
经过一些试验,我确认我遇到的问题是api使用owin中间件验证访问令牌。
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中看不到,这可能是我未获得令牌验证的原因吗?
答案 0 :(得分:0)
首先尝试在从“ mvc”到“ client2”
的这一行上使用正确的client_idvar tokenClient =新的TokenClient(“ http://localhost:5000/connect/token”,“ mvc” ,“秘密”);