在Web API 2中,您曾经能够通过中间件设置OAuth授权服务器来创建端点以发出令牌,如下所示:
//Set up our auth server options.
var OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new SimpleAuthorizationServerProvider()
};
// Sets up the token issue endpoint using the options above
app.UseOAuthAuthorizationServer(OAuthServerOptions);
Run Code Online (Sandbox Code Playgroud)
也许我错过了它,但我想弄清楚如何在ASP.NET Core中做到这一点.我查看了源代码(https://github.com/aspnet/Security),但我真的没有看到类似的东西.有没有新的方法来实现这一目标?我是否需要创建一个控制器并自己完成?
我看到如何通过中间件设置OAuth身份验证,但这涉及我从API发出声明的授权部分.
我在默认的Visual Studio 2015 ASP.NET 5项目的Startup.cs文件中找到了这个ConfigureServices:
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AuthorizationDbContext>()
.AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)
它究竟做了什么,以及如何使用这些"默认提供商"?它是否为我配置了所有基于令牌的身份验证?我在哪里可以阅读更多相关信息?
我正在尝试使用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
Pinpoint帮助我从发射台上取下这个原型 - 我非常接近,除了:
{
"projects": [ "src", "test" ],
"sdk": {
"version": "1.0.0-beta6"
}
}
Run Code Online (Sandbox Code Playgroud)
{
"webroot": "wwwroot",
"version": "1.0.0-*",
"dependencies": {
"Microsoft.AspNet.Mvc": "6.0.0-beta6",
"Microsoft.AspNet.Server.IIS": "1.0.0-beta6",
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta6",
"Microsoft.AspNet.StaticFiles": "1.0.0-beta6",
"System.IdentityModel.Tokens": "5.0.0-beta6-207211625",
"Serilog.Framework.Logging": "1.0.0-beta-43",
"Microsoft.AspNet.Authentication.OAuthBearer": "1.0.0-beta6"
},
"commands": {
"web": "Microsoft.AspNet.Hosting --config hosting.ini"
},
"frameworks": {
"dnx451": { }
},
"exclude": [
"wwwroot",
"node_modules",
"bower_components"
],
"publishExclude": [
"node_modules",
"bower_components",
"**.xproj",
"**.user",
"**.vspscc"
]
}
Run Code Online (Sandbox Code Playgroud)
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{ …Run Code Online (Sandbox Code Playgroud) 我即将使用ASP.NET Core框架实现Web应用程序(现有应用程序的后台办公室).目前我正在考虑如何验证用户身份.主数据库使用MySQL(此框架不支持),因此我将通过现有的PHP SOAP API访问数据库.
有没有办法实现基于令牌的(OAuth)身份验证,知道数据访问将通过SOAP API完成?
在过去的7天中,我尝试使用OpenIdConnect.Server资源所有者流来设置ASP.NET 5 WebApi 。
我或多或少成功地生成了令牌并访问了[Authorize]受保护的操作。
但是,当我尝试访问时this.User.Identity.Claims,它是空的。我现在正在使用ASP.NET 5,beta6(在升级到最新的beta7并等待其正式发布时遇到了麻烦)
在Startup.cs中,我得到了以下内容:
public void ConfigureServices(IServiceCollection services)
{
services.AddCaching();
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<AuthContext>(options =>
{
options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString"));
});
services.AddIdentity<AuthUser, AuthRole>(
options => options.User = new Microsoft.AspNet.Identity.UserOptions
{
RequireUniqueEmail = true,
UserNameValidationRegex = "^[a-zA-Z0-9@_\\.-]+$"
})
.AddEntityFrameworkStores<AuthContext, Guid>()
.AddDefaultTokenProviders();
services.ConfigureCors(configure =>
{
configure.AddPolicy("CorsPolicy", builder =>
{
builder.WithOrigins("http:/localhost/", "http://win2012.bludev.com/");
});
});
services.AddScoped<IAuthRepository, AuthRepository>();
}
public void Configure(IApplicationBuilder app)
{
var factory = app.ApplicationServices.GetRequiredService<ILoggerFactory>();
factory.AddConsole();
app.UseStaticFiles();
app.UseOAuthBearerAuthentication(options =>
{
options.Authority = "http://win2012.bludev.com/api/auth/";
options.Audience = "http://win2012.bludev.com/"; …Run Code Online (Sandbox Code Playgroud) c# claims-based-identity openid-connect aspnet-contrib asp.net-core
阅读这个问题,@ Pinpoint的回答以及对评论的进一步讨论,我很清楚,我们原本无法在使用ASP.NET 5开发的应用程序中添加身份提供程序.然后,可以通过以下方式提供对旧版OAuthAuthorizationServerMiddleware的替代.我在许多地方找到的AspNet.Security.OpenIdConnect.Server.
现在,有一点我仍然不确定这一切因为我真的不是安全方面的专家,所以我对OAuth的了解不是很深刻.我怀疑如下:在使用OAuth保护一个RESTful API时是否可以使用外部身份提供程序?
请注意,我不是在谈论将社交登录添加到一个网站,我在谈论在一个RESTful API中使用一个外部身份提供程序.
我的观点是,这让我有点困惑,因为我一直认为这应该是我的应用程序的一个问题.
所以我的问题是:在使用OAuth和ASP.NET 5时,是否可以使用外部身份提供程序,而不是实现一个?如果有可能,这简单如何工作?我的意思是,我的应用程序仍然需要能够管理用户的身份,因为它需要管理声明等等.
在这种情况下,如果真的有可能,流程将如何?外部身份提供商应该发行令牌吗?但我的应用程序如何能够验证这些令牌并管理用户身份?
编辑:我不确定的原因之一是,当我们使用UseOAuthAuthentication扩展方法时,我们设置了一个回调路径,描述为
应用程序基本路径中将返回用户代理的请求路径.中间件将在到达时处理此请求.
现在,如果我们正在开发一个网站,那么这确实有意义.该人去那里,点击按钮登录Facebook等提供商.用户被重定向到Facebook的页面,然后在他登录后,他被重定向到该站点的某个页面.
另一方面,使用RESTful API,这是毫无意义的.没有被重定向的概念.
这使得外部提供程序的使用似乎仅适用于站点,而不适用于RESTful API.这是我的问题的要点.
基于Shaun Luttin在/sf/answers/2160026711/上的一个很好的例子,我能够使用该代码生成和使用承载令牌.微小的变化是获得最新的包:
"dependencies": {
"Microsoft.AspNet.Authentication.JwtBearer": "1.0.0-rc1-final",
"AspNet.Security.OpenIdConnect.Server": "1.0.0-beta4"
}
Run Code Online (Sandbox Code Playgroud)
虽然代码是一个很好的开始,但它并不是一个完全集成w/ASP.NET身份的完整解决方案.我修改了AuthorizationProvider类,如下所示:
public override Task GrantResourceOwnerCredentials(
GrantResourceOwnerCredentialsContext context)
{
var user = _userManager.FindByNameAsync(context.UserName).Result;
if (user == null)
{
context.Rejected("The user name or password is incorrect.");
}
else
{
var signInManager = context.HttpContext.RequestServices
.GetRequiredService<SignInManager<ApplicationUser>>();
if (signInManager.CanSignInAsync(user).Result &&
_userManager.CheckPasswordAsync(user, context.Password).Result)
{
var principal = signInManager.CreateUserPrincipalAsync(user).Result;
//To avoid leaking confidential data, AspNet.Security.OpenIdConnect.Server
//refuses to serialize the claims that don't explicitly specify a destination.
foreach (var claim in principal.Claims)
claim.WithDestination("token id_token");
context.Validated(principal);
}
else …Run Code Online (Sandbox Code Playgroud) bearer-token openid-connect asp.net-identity-3 aspnet-contrib asp.net-core
与此帖子相关的问题:配置授权服务器端点.
使用上面的例子,我可以得到令牌.以前可以通过骑马获得额外的信息
public override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
{
context.AdditionalResponseParameters.Add(property.Key, property.Value);
}
return Task.FromResult<object>(null);
}
Run Code Online (Sandbox Code Playgroud)
你如何在当前的实施中实现这一目标
public override Task TokenEndpoint(TokenEndpointContext context){
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
有人可以解释Identity和IdentityServer之间的联系吗?
最近有一篇文章说将有一个新版本的IdentityServer(即IdentityServer 4).
我正在做一个ASP.NET 5项目.我以前只听说过Identity 3,我打算用它.
我可以在不使用此IdentityServer的情况下在项目中使用Identity 3吗?
我正在使用ASP.Net MVC6开发一个应用程序,我想使用bearer令牌实现OAuth 2 auth.关于这是否可行,我找不到任何可靠的信息.有人能指出我正确的方向吗?
使用 ASP.NET Core 1.0 (rtm),以及我生成的最小可验证示例,我发现我生成的 JSON Web 令牌没有通过 .net core json Web 令牌中间件声明挑战,并且因此失败错误:
Microsoft.IdentityModel.Tokens.SecurityTokenInvalidSignatureException:
IDX10504: Unable to validate signature, token does not have a signature:
'... ... token value here...'
Run Code Online (Sandbox Code Playgroud)
我制作了一个最小的示例,它在 ASP.NET Core RC1 中运行良好,但在 RTM 中不起作用。我没有移植到RC2上进行测试,但我相信这样的测试是没有意义的。您可以使用提供的测试脚本来练习演示:
python tests\testloginapi.py
GET OK: 200 http://localhost:54993/authorize/login?username=TEST&pas...
POST OK: 200 http://localhost:54993/authorize/login?username=TEST&pas...
authorization token received... eyJhbGciOi...
expected status 200 but got 401 for GET http://localhost:54993/authorizetest/test
Run Code Online (Sandbox Code Playgroud)
我的最小示例的要点是:
Startup.cs方法Configure有:
app.UseJwtBearerAuthentication(_keyArtifacts.getJwtBearerOptions());
Run Code Online (Sandbox Code Playgroud)
承载选项如下:
public static JwtBearerOptions CreateJwtBearerOption(RsaSecurityKey key, string tokenIssuer, string tokenAudience)
{
var jwtBearerOptions = new …Run Code Online (Sandbox Code Playgroud) asp.net-core ×10
asp.net ×5
c# ×3
jwt ×2
oauth ×2
bearer-token ×1
mysql ×1
oauth-2.0 ×1
openid ×1
rest ×1