我正在尝试使用AspNew.Security.OpenIdConnect.Server来发布令牌并使用Microsoft.AspNetCore.Authentication.JwtBearer进行验证,从而使一个简单的端点正常工作,从而解决并使用JWT令牌.
我可以生成令牌,但尝试验证令牌失败并显示错误 Bearer was not authenticated. Failure message: No SecurityTokenValidator available for token: {token}
在这一点上,我已经删除了所有内容并具有以下内容:
project.json
{
"dependencies": {
"Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
"AspNet.Security.OAuth.Validation": "1.0.0-alpha1-final",
"AspNet.Security.OpenIdConnect.Server": "1.0.0-beta5-final",
"Microsoft.AspNetCore.Authentication": "1.0.0-rc2-final",
"Microsoft.AspNetCore.Authentication.JwtBearer": "1.0.0-rc2-final"
},
"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": {
"version": "1.0.0-preview1-final",
"imports": "portable-net45+win8+dnxcore50"
}
},
"frameworks": {
"net461": { }
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"publishOptions": {
"include": [
"wwwroot",
"Views",
"appsettings.json",
"web.config"
]
},
"scripts": …Run Code Online (Sandbox Code Playgroud) authentication jwt openid-connect aspnet-contrib asp.net-core
我已使用和使用"Microsoft.AspNetCore.Authentication.OpenIdConnect":"1.0.0和"Microsoft.AspNetCore.Authentication.Cookies":"1.0.0"的asp.net核心mvc应用程序配置了ASOS OpenIdConnect服务器.我已经测试了"授权代码"工作流程,一切正常.
客户端Web应用程序按预期处理身份验证,并创建一个存储id_token,access_token和refresh_token的cookie.
如何强制Microsoft.AspNetCore.Authentication.OpenIdConnect在过期时请求新的access_token?
asp.net核心mvc应用程序忽略过期的access_token.
我想让openidconnect看到过期的access_token然后使用刷新令牌进行调用以获得新的access_token.它还应该更新cookie值.如果刷新令牌请求失败,我希望openidconnect"注销"cookie(删除它或其他东西).
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
AuthenticationScheme = "Cookies"
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
ClientId = "myClient",
ClientSecret = "secret_secret_secret",
PostLogoutRedirectUri = "http://localhost:27933/",
RequireHttpsMetadata = false,
GetClaimsFromUserInfoEndpoint = true,
SaveTokens = true,
ResponseType = OpenIdConnectResponseType.Code,
AuthenticationMethod = OpenIdConnectRedirectBehavior.RedirectGet,
Authority = http://localhost:27933,
MetadataAddress = "http://localhost:27933/connect/config",
Scope = { "email", "roles", "offline_access" },
});
Run Code Online (Sandbox Code Playgroud) 我正在使用AspNet.Security.OpenIdConnect.Server来发出JWT令牌,并将AuthorizationCodeLifetime设置为30秒进行测试.这是我用来设置选项的代码片段
options.TokenEndpointPath = "/api/token";
options.AllowInsecureHttp = true;
options.AccessTokenHandler = new JwtSecurityTokenHandler();
options.SigningCredentials.Add(new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256));
options.AccessTokenLifetime = TimeSpan.FromSeconds(30);
options.AuthorizationCodeLifetime = TimeSpan.FromSeconds(30);
Run Code Online (Sandbox Code Playgroud)
返回的令牌包含:
"expires_in": 30,
Run Code Online (Sandbox Code Playgroud)
并且反序列化的令牌包含以下声明:
"nbf": 1476915220,
"exp": 1476915250,
"iat": 1476915220,
Run Code Online (Sandbox Code Playgroud)
如您所见,exp(过期时间)是在iat(发布时间)之后30秒.然而,令牌在到期后5分钟才触发401 Unauthorized.如果我们将exp号码插入http://www.epochconverter.com/,那么我们将看到这个评估结果为2016年10月19日星期三格林威治标准时间22:14:10,当地时间是下午5:14:10.
以下是Core库的一些记录输出:
Microsoft.IdentityModel.Tokens.SecurityTokenExpiredException: IDX10223: Lifetime validation failed. The token is expired.
ValidTo: '10/19/2016 22:14:10'
Current time: '10/19/2016 22:19:10'.
at Microsoft.IdentityModel.Tokens.Validators.ValidateLifetime(Nullable`1 notBefore, Nullable`1 expires, SecurityToken securityToken, TokenValidationParameters validationParameters)
at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateLifetime(Nullable`1 notBefore, Nullable`1 expires, JwtSecurityToken securityToken, TokenValidationParameters validationParameters)
at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateToken(String token, TokenValidationParameters validationParameters, SecurityToken& validatedToken)
at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.<HandleAuthenticateAsync>d__1.MoveNext()
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[7] …Run Code Online (Sandbox Code Playgroud) 所以我对我的project.json进行了更改,导致重新恢复,这会产生一堆无法解析的依赖项.你怎么知道这里发生了什么?这肯定是有效的,因为我写了很多针对这个project.json文件的代码.
"dependencies": {
"EntityFramework.Commands": "7.0.0-*",
"Microsoft.AspNet.Authentication.Cookies": "1.0.0-*",
"Microsoft.AspNet.Diagnostics.Entity": "7.0.0-*",
"Microsoft.AspNet.Identity.EntityFramework": "3.0.0-*",
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-*",
"Microsoft.AspNet.Authentication.JwtBearer": "1.0.0-rc2-*",
"Microsoft.AspNet.Mvc": "6.0.0-*",
"Microsoft.AspNet.Hosting": "1.0.0-*",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-*",
"Microsoft.AspNet.StaticFiles": "1.0.0-*",
"Microsoft.Extensions.Configuration.Json": "1.0.0-*",
"AspNet.Security.OAuth.Validation": "1.0.0-*",
"OpenIddict": "1.0.0-*",
"System.IdentityModel.Tokens.Jwt": "5.0.0-rc2-301150021",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc2-15958",
"Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-15958",
"EntityFramework.Sqlite": "7.0.0-rc2-*",
"EntityFramework.Sqlite.Design": "7.0.0-rc1-final",
"Microsoft.Extensions.PlatformAbstractions": "1.0.0-*"
}
NotFound https://api.nuget.org/v3-flatcontainer/microsoft.aspnetcore.staticfiles/index.json 514ms
NotFound https://api.nuget.org/v3-flatcontainer/microsoft.aspnetcore.identity.entityframeworkcore/index.json 498ms
NotFound https://api.nuget.org/v3-flatcontainer/microsoft.aspnetcore.hosting.abstractions/index.json 1743ms
NotFound https://api.nuget.org/v3-flatcontainer/microsoft.aspnetcore.authentication/index.json 1745ms
NotFound https://api.nuget.org/v3-flatcontainer/microsoft.extensions.fileproviders.embedded/index.json 1791ms
NotFound https://api.nuget.org/v3-flatcontainer/microsoft.extensions.fileproviders.composite/index.json 1859ms
NotFound https://api.nuget.org/v3-flatcontainer/microsoft.aspnetcore.identity/index.json 1892ms
NotFound https://api.nuget.org/v3-flatcontainer/microsoft.aspnetcore.cors/index.json 1901ms
NotFound https://api.nuget.org/v3-flatcontainer/microsoft.aspnetcore.mvc/index.json 1875ms
NotFound https://api.nuget.org/v3-flatcontainer/microsoft.aspnetcore.hosting/index.json 1887ms
NotFound https://api.nuget.org/v3-flatcontainer/openiddict/index.json 1720ms
Run Code Online (Sandbox Code Playgroud) AspNet.Security.OpenIdConnect.Server上的示例看起来像auth和资源服务器.我想把它们分开.我已经这样做了.
在auth服务器的Startup.Config中,我有以下设置:
app.UseOpenIdConnectServer(options => {
options.AllowInsecureHttp = true;
options.ApplicationCanDisplayErrors = true;
options.AuthenticationScheme = OpenIdConnectDefaults.AuthenticationScheme;
options.Issuer = new System.Uri("http://localhost:61854"); // This auth server
options.Provider = new AuthorizationProvider();
options.TokenEndpointPath = new PathString("/token");
options.UseCertificate(new X509Certificate2(env.ApplicationBasePath + "\\mycertificate.pfx","mycertificate"));
});
Run Code Online (Sandbox Code Playgroud)
我有一个AuthorizationProvider编写,但我不认为它与我当前的问题相关(但可能相关).在GrantResourceOwnerCredentials覆盖中,我对声明主体进行了硬编码,以便验证每个令牌请求:
public override Task GrantResourceOwnerCredentials(GrantResourceOwnerCredentialsNotification context)
{
var identity = new ClaimsIdentity(OpenIdConnectDefaults.AuthenticationScheme);
identity.AddClaim(ClaimTypes.Name, "me");
identity.AddClaim(ClaimTypes.Email, "me@gmail.com");
var claimsPrincipal = new ClaimsPrincipal(identity);
context.Validated(claimsPrincipal);
return Task.FromResult<object>(null);
}
Run Code Online (Sandbox Code Playgroud)
在资源服务器上,我在其Startup.config中有以下内容:
app.UseWhen(context => context.Request.Path.StartsWithSegments(new PathString("/api")), branch =>
{
branch.UseOAuthBearerAuthentication(options => {
options.Audience = "http://localhost:54408"; // This resource …Run Code Online (Sandbox Code Playgroud) oauth asp.net-web-api openid-connect aspnet-contrib asp.net-core
我正在使用Visual Studio 2015 Enterprise和ASP.NET vNext Beta8构建一个既发布又使用JWT令牌的端点.我最初由生成令牌自己走近这个,描述在这里.后来@Pinpoint的一篇有用的文章显示,AspNet.Security.OpenIdConnect.Server(又名OIDC)可以配置为我发布和使用令牌.
所以我按照这些说明,站起来,通过从邮递员提交x-www-form-urlencoded帖子,我收到了一个合法的令牌:
{
"token_type": "bearer",
"access_token": "eyJ0eXAiO....",
"expires_in": "3599"
}
Run Code Online (Sandbox Code Playgroud)
这很棒,但也是我卡住的地方.现在,如何注释控制器操作以便它需要此持有者令牌?
我认为我所要做的就是用[Authorize("Bearer")]装饰我的控制器方法,添加一个认证方案:
services.AddAuthorization
(
options =>
{
options.AddPolicy
(
JwtBearerDefaults.AuthenticationScheme,
builder =>
{
builder.
AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme).
RequireAuthenticatedUser().
Build();
}
);
}
);
Run Code Online (Sandbox Code Playgroud)
然后像我在前面的例子中所做的那样用"授权持有者eyJ0eXAiO ...."标题调用我的控制器动作.可悲的是,所有这些方法似乎都会产生异常:
处理请求时发生未处理的异常.
SocketException:无法建立连接,因为目标计算机主动拒绝它127.0.0.1:50000
WebException:无法连接到远程服务器
HttpRequestException:发送请求时发生错误.
IOException:IDX10804:无法从以下位置检索文档:' http:// localhost:50000/.well-known/openid-configuration '.Microsoft.IdentityModel.Logging.LogHelper.Throw(String message,Type exceptionType,EventLevel logLevel,Exception innerException)
InvalidOperationException:IDX10803:无法从以下位置获取配置:' http:// localhost:50000/.well-known/openid-configuration '.内部异常:'IDX10804:无法从以下位置检索文档:' http:// localhost:50000/.well-known/openid-configuration '.'.
请考虑以下步骤来重现(但请不要考虑这个生产有价值的代码):
描述应用ASP.NET Beta8工装这里
打开Visual Studio Enterprise 2015并创建新的Web API ASP.NET 5预览模板项目
改变project.json
{
"根目录": "wwwroot的", …
我试图找到一种方法,使我的API能够将Facebook用户与我的身份用户相关联.
应用程序上下文
我正在开发一个移动应用程序(在Xamarin中),需要使用用户名/密码和Facebook进行登录.我已经设置了app.UseOpenIdConnectServer配置并创建了自定义,Provider因此我的应用程序已经在使用用户名/密码.
现在我正试图与Facebook进行这种集成,而不是找到一种方法来实现这一目标.
我正在考虑在API中创建服务,例如从Facebook /api/auth/login-facebook/传递,access-token但我需要将access-token我的API应用程序返回到移动应用程序,以便移动应用程序可以调用所有需要授权的其他服务.
对此有何帮助?
我想要获得的视觉方式:
/api/auth/login-facebook/传递access-tokenaccess-token与Facebook包access-token授予访问我的API应用程序的权限access-token授予访问我的API应用程序的权限access-token移动应用程序,以便可以调用其他服务如果我的知识有误并且我应该以其他方式进行此集成/登录,请随意告诉我!
我将 ASP.NET Core 与 OpenIddict、JWT、资源所有者授予和基于声明的角色一起使用。不执行任何策略的授权正在按预期进行。
我想对某些控制器和操作方法强制执行授权策略。我的所有用户都有角色声明,因此我在启动中执行了以下操作:
services.AddAuthorization(options =>
{
options.AddPolicy("Admin", p => p.RequireClaim("Admin");
});
Run Code Online (Sandbox Code Playgroud)
我对操作方法做了以下操作:
[Authorize("Admin")]
public async Task<string> Index()
{
return "Yes";
}
Run Code Online (Sandbox Code Playgroud)
没有“管理员”,我能够访问该资源,添加“管理员”后我不能。
我假设是因为我生成的 JWT 令牌没有用户声明。
将 ASP.NET Core 与 OpenIddict 密码授予结合使用。
当调用身份验证端点时,我得到以下信息:
{
"token_type": "Bearer",
"access_token": "eyJhbGciOiJ...",
"expires_in": 1800
}
Run Code Online (Sandbox Code Playgroud)
如何在响应中包含用户 ID?我可以在解码的令牌中看到它,但我的用户应用程序不会对其进行解码。
asp.net-core-mvc openid-connect aspnet-contrib asp.net-core openiddict
在一个旧项目中,我们使用了 ASP.NET 4.5,我想在其中使用框架 OpenIdDict。它是由 ASP.NET Core 1 和 2 制作的。我还能使用它吗?我需要注意什么?如果我不能使用它,你知道哪些替代方案?
OpenIdDict 链接:https : //github.com/openiddict/openiddict-core
aspnet-contrib ×10
asp.net-core ×9
jwt ×4
openiddict ×4
c# ×2
asp.net ×1
cookies ×1
facebook ×1
oauth ×1