我知道有类似的问题,但还不太清楚,在阅读了一堆与主题相关的帖子后,我“理解”了代码应该是什么样子,我仍然在处理oauth/openid/owin/katana/identityserver等所有的概念.
总体情况是:我有一个有角度的应用程序,其中用户注册和登录,无需同意,一旦用户登录,SPA将开始与后面的所有api通信,api应该能够在auth服务器上进行身份验证。
因此,基本上,我需要我的web能够通过客户端凭据授予类型,通过身份验证服务器发出的令牌,在标识服务器4中进行身份验证。
我在标识服务器4中定义了这个客户机(web 2 .net Framework4.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,
}
}); 控制器用自动装潢器装饰。这些是im使用的包的版本。
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" },
});
}我正在使用Idtyserver3.assistokenValidation来执行验证,因为它是被重命名的,但是在获得客户端应用程序中的访问令牌并将其传递给api请求之后,我得到了一个401未经授权的错误,这是否是因为它期望在安全的HTTPS下操作?我注意到对于访问验证v4,您可以设置"RequireHttpsMetadata = false“,但我在v3中没有看到这一点,这可能是我没有得到令牌验证的原因吗?
发布于 2018-11-16 14:55:40
尝试在从"mvc“到"client2"的这一行中首先使用正确的client2
var tokenClient =新TokenClient("http://localhost:5000/connect/token","mvc",“秘”);
https://stackoverflow.com/questions/53313001
复制相似问题