Authentication 如何使用/验证AspNet.Security.OpenIdConnect.Server(RC1)颁发的令牌?

Authentication 如何使用/验证AspNet.Security.OpenIdConnect.Server(RC1)颁发的令牌?,authentication,oauth-2.0,asp.net-core,openid-connect,aspnet-contrib,Authentication,Oauth 2.0,Asp.net Core,Openid Connect,Aspnet Contrib,我遵循了从帖子中了解的关于如何实现AspNet.Security.OpenIdConnect.Server的所有内容 精确定位,你听到了吗?;) 我已经设法将代币发行和代币消费分开。我不会显示“身份验证服务器端”,因为我认为这部分已经设置好了,但我将显示如何在自定义授权提供程序中构建身份验证票证: 公共密封类授权提供程序:OpenIdConnectServerProvider { //其他覆盖未显示。我已将其放宽,以始终验证。 公共重写异步任务GrantResourceOwnerCredenti

我遵循了从帖子中了解的关于如何实现AspNet.Security.OpenIdConnect.Server的所有内容

精确定位,你听到了吗?;)

我已经设法将代币发行和代币消费分开。我不会显示“身份验证服务器端”,因为我认为这部分已经设置好了,但我将显示如何在自定义授权提供程序中构建身份验证票证:

公共密封类授权提供程序:OpenIdConnectServerProvider
{
//其他覆盖未显示。我已将其放宽,以始终验证。
公共重写异步任务GrantResourceOwnerCredentials(GrantResourceOwnerCredentialsContext)
{
//我正在使用Microsoft.AspNet.Identity验证用户/密码。
//那么,假设我已经从
//用户管理器UM:
var identity=newclaimsidentity(OpenIdConnectServerDefaults.AuthenticationScheme);
//AddClaims(等待UM.GetClaimsAsync(用户));
identity.AddClaim(ClaimTypes.Name、user.UserName);
(等待UM.GetRolesAsync(用户)).ToList().ForEach(角色=>{
identity.AddClaim(ClaimTypes.Role,Role);
});
var票证=新身份验证票证(新身份证),
新建AuthenticationProperties(),
context.Options.AuthenticationScheme);
//根据我最近的研究,有些新东西
SetResources(新[]{“我的资源服务器”});
设置访问群体(新[]{“我的资源服务器”});
票证设置范围(新[]{“defaultscope”});
上下文。已验证(票证);
}
}
并在身份验证服务器上启动:

使用系统;
使用Microsoft.AspNet.Builder;
使用Microsoft.AspNet.Hosting;
使用Microsoft.Data.Entity;
使用Microsoft.Extensions.DependencyInjection;
使用MyAuthServer.Providers;
命名空间My.AuthServer
{
公营创业
{
public void配置服务(IServiceCollection服务)
{
services.AddAuthentication();
services.AddCaching();
services.AddMvc();
string connectionString=“实际上有一个”;
services.AddEntityFramework()
.AddSqlServer()文件
.AddDbContext(选项=>{
options.UseSqlServer(connectionString.UseRowNumberForPaging();
});
服务.额外性()
.AddEntityFrameworkStores().AddDefaultTokenProviders();
}
公共void配置(IApplicationBuilder应用程序)
{
app.UseIISPlatformHandler();
app.UseOpenIdConnectServer(选项=>{
options.ApplicationCanDisplayErrors=true;
options.AllowInsecureHttp=true;
options.Provider=newauthorizationprovider();
options.TokenEndpointPath=“/token”;
options.AccessTokenLifetime=新的时间跨度(1,0,0,0);
options.Issuer=新Uri(“http://localhost:60556/");
});
app.UseMvc();
app.usewlcomepage();
}
publicstaticvoidmain(字符串[]args)=>WebApplication.Run(args);
}
}
果然,当我有这个HTTP请求时,我确实得到了一个访问令牌,但我不确定该访问令牌是否具有资源服务器所期望的所有数据

POST /token HTTP/1.1
Host: localhost:60556
Content-Type: application/x-www-form-urlencoded

username=admin&password=pw&grant_type=password
现在,在资源服务器端,我使用JWT承载身份验证。在启动时,我有:

使用Microsoft.AspNet.Builder;
使用Microsoft.AspNet.Hosting;
使用Microsoft.Data.Entity;
使用Microsoft.Extensions.DependencyInjection;
命名空间MyResourceServer
{
公营创业
{            
public void配置服务(IServiceCollection服务)
{
services.AddMvc();
string connectionString=“实际上有一个”;
services.AddEntityFramework()
.AddSqlServer()文件
.AddDbContext(选项=>{
options.UseSqlServer(connectionString.UseRowNumberForPaging();
});
服务.额外性()
.AddEntityFrameworkStores().AddDefaultTokenProviders();
}
公共void配置(IApplicationBuilder应用程序)
{
app.UseIISPlatformHandler();
app.UseMvc();
app.usewlcomepage();
app.UseJwtBearerAuthentication(选项=>{
options.acquisition=“我的资源服务器”;
选项。权限=”http://localhost:60556/"; 
options.AutomaticAuthenticate=true;
options.RequireHttpsMetadata=false;
});
}
publicstaticvoidmain(字符串[]args)=>WebApplication.Run(args);
}
}
当我向资源服务器发出此HTTP请求时,我得到一个401未经授权的消息:

GET /api/user/myroles HTTP/1.1
Host: localhost:64539
Authorization: Bearer eyJhbGciOiJS...
Content-Type: application/json;charset=utf-8
具有到
/api/user/myroles
的路由的控制器用不带参数的普通
[Authorize]
修饰

我觉得我在auth和资源服务器中都遗漏了一些东西,但不知道它们是什么

其他问题“如何验证AspNet.Security.OpenIdConnect.Server颁发的令牌”没有答案。在这方面我会很感激你的帮助

另外,我注意到示例提供程序中注释了OAuth内省,并且在某个地方读到Jwt不会很快得到支持。我找不到提供OAuth说明的依赖项


更新我已经包括了我的startup.cs,分别来自auth和资源服务器。是否有任何错误会导致资源服务器始终停止运行
app.UseIISPlatformHandler();

app.UseJwtBearerAuthentication(options => {
    options.Audience = "my_resource_server";  
    options.Authority = "http://localhost:60556/"; 
    options.AutomaticAuthenticate = true;
    options.RequireHttpsMetadata = false;
});

app.UseMvc();
app.UseWelcomePage();
var identity = new ClaimsIdentity(OpenIdConnectServerDefaults.AuthenticationScheme);
identity.AddClaim(ClaimTypes.Name, user.UserName, destination: "id_token token");

(await UM.GetRolesAsync(user)).ToList().ForEach(role => {
    identity.AddClaim(ClaimTypes.Role, role, destination: "id_token token");
});
app.UseOAuthValidation();

// Alternatively, you can also use the introspection middleware.
// Using it is recommended if your resource server is in a
// different application/separated from the authorization server.
// 
// app.UseOAuthIntrospection(options => {
//     options.AutomaticAuthenticate = true;
//     options.AutomaticChallenge = true;
//     options.Authority = "http://localhost:54540/";
//     options.Audience = "resource_server";
//     options.ClientId = "resource_server";
//     options.ClientSecret = "875sqd4s5d748z78z7ds1ff8zz8814ff88ed8ea4z4zzd";
// });