ASP.NET 5 OAuth承载令牌认证
我试图在ASP.NET 5中实现OAuth持票人身份验证,并且正在努力寻找如何做到这一点的示例,因为OWIN的东西在ASP.NET 5中发生了变化。
例如IApplicationBuilder.UseOAuthAuthorizationServer()和IApplicationBuilder。 UseOAuthBearerAuthentication()或者不再存在,或者我缺少一个引用?
任何指针将不胜感激。
我做了它的工作,但是将Thinktecture的身份服务器v 3设置为我的令牌提供程序,但是我认为如果您有另一个令牌提供程序,它将会是相同的流程....
(更新:我添加了代码的github回购:这里)
这里是我的启动班:(Identityserver v3也可以在Vnext上运行一些小小的调整)。 请注意我在同一个web应用程序中有服务器和web api。 如果你有两个不同的web项目也可以,但这里是为了演示...
public class Startup
{
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
app.Map("/core", core =>
{
var factory = InMemoryFactory.Create(
users: Users.Get(),
clients: Clients.Get(),
scopes: Scopes.Get());
var idsrvOptions = new IdentityServerOptions
{
IssuerUri = "https://idsrv3.com",
SiteName = "test vnext Identity server",
Factory = factory,
SigningCertificate = Certificate.Get(),
RequireSsl = false,
CorsPolicy = CorsPolicy.AllowAll,
AuthenticationOptions = new AuthenticationOptions
{
}
};
core.UseIdentityServer(idsrvOptions);
});
app.Map("/api", api =>
{
api.UseOAuthBearerAuthentication(options => {
options.Authority = Constants.AuthorizationUrl;
options.MetadataAddress = Constants.AuthorizationUrl + "/.well-known/openid-configuration";
options.TokenValidationParameters.ValidAudience = "https://idsrv3.com/resources";
});
api.UseMvc();
});
}
}
从这里你可以看到我的IdentityServerV3映射到'/ core',并且在同一个web app项目中(它可能是另一个),我有一个使用MVC的web API。 下面是控制器:
[Authorize]
[Route("[controller]")]
public class Test : Controller
{
[HttpGet]
public JsonResult Get()
{
return Json(new
{
message = "You See this then it's ok auth is :" + User.Identity.IsAuthenticated,
});
}
}
我在身份服务器中配置了一个客户端:
new Client
{
//Resource Owner Flow Client (our web UI)
ClientName = "WebUI",
Enabled = true,
ClientId = "IdentityWebUI",
ClientSecrets = new List<ClientSecret>
{
new ClientSecret("secret".Sha256())
},
Flow = Flows.ResourceOwner,
AccessTokenType = AccessTokenType.Jwt,
AccessTokenLifetime = 3600
}
这里是User(用于InMemory用户):
return new List<InMemoryUser>
{
new InMemoryUser
{
Username = "testUser",
Password = "testPwd",
Subject = "I am the Subject"
}
};
在fidler中,我发出以下POST来获得不记名令牌:
POST : http://localhost:4357/core/connect/token
User-Agent: Fiddler
Host: localhost:4357
Content-Length: 67
Content-Type: application/x-www-form-urlencoded
Authorization: Basic SWRlbnRpdHlXZWJVSTpzZWNyZXQ=
grant_type=password&username=testUser&password=testPwd&scope=openid
在响应中您将获得一个Access_token
{"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImEzck1VZ01Gdjl0UGNsTGE2eUYzekFrZnF1RSIsImtpZCI6ImEzck1VZ01Gdjl0UGNsTGE2eUYzekFrZnF1RSJ9.eyJjbGllbnRfaWQiOiJJZGVudGl0eVdlYlVJIiwic2NvcGUiOiJvcGVuaWQiLCJzdWIiOiJJIGFtIHRoZSBTdWJqZWN0IiwiYW1yIjoicGFzc3dvcmQiLCJhdXRoX3RpbWUiOjE0MjgzOTQ3MzAsImlkcCI6Imlkc3J2IiwiaXNzIjoiaHR0cHM6Ly9pZHNydjMuY29tIiwiYXVkIjoiaHR0cHM6Ly9pZHNydjMuY29tL3Jlc291cmNlcyIsImV4cCI6MTQyODM5ODMzMCwibmJmIjoxNDI4Mzk0NzMwfQ.cbB4YrRXaaRDNw8BjeI4Q1DvXN28xmJScMJBGWCM_zSLcH1i63cQVTmR8X86rGP5VrR0Ly4-EmWZ8911Vh4jc4Ua0Kgz2n7RbmQ6VqQX5Z_lM3F8EIgD81kpUn0v3hhSFW06aJ2Lo1XOZG_re84xGgqre-H4dC0XZR6IQMEAQ9Q5dOXBh8V1NxyLSh0PzyrRRmOnEndoaY4uaIFtbp9j7KnXxQ3ZdGmaYAO96xuhHfO1DbgRdw6fYyf4nnC795yhnwDh1QZGxPsFaysJSA_3-cjmw-29m-Ga0hD1ALfVE7R57iNLxkB6dyEuz1UFJhJyibRDW9sNspo2gQFZZGxMKQ","expires_in":3600,"token_type":"Bearer"}
然后我使用access_token来调用我的web api
这里是小提琴手(在作曲家窗格中)
GET http://localhost:4357/api/Test
User-Agent: Fiddler
Host: localhost:4357
Content-Length: 0
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6ImEzck1VZ01Gdjl0UGNsTGE2eUYzekFrZnF1RSIsImtpZCI6ImEzck1VZ01Gdjl0UGNsTGE2eUYzekFrZnF1RSJ9.eyJjbGllbnRfaWQiOiJJZGVudGl0eVdlYlVJIiwic2NvcGUiOiJvcGVuaWQiLCJzdWIiOiJJIGFtIHRoZSBTdWJqZWN0IiwiYW1yIjoicGFzc3dvcmQiLCJhdXRoX3RpbWUiOjE0MjgzOTQ3MzAsImlkcCI6Imlkc3J2IiwiaXNzIjoiaHR0cHM6Ly9pZHNydjMuY29tIiwiYXVkIjoiaHR0cHM6Ly9pZHNydjMuY29tL3Jlc291cmNlcyIsImV4cCI6MTQyODM5ODMzMCwibmJmIjoxNDI4Mzk0NzMwfQ.cbB4YrRXaaRDNw8BjeI4Q1DvXN28xmJScMJBGWCM_zSLcH1i63cQVTmR8X86rGP5VrR0Ly4-EmWZ8911Vh4jc4Ua0Kgz2n7RbmQ6VqQX5Z_lM3F8EIgD81kpUn0v3hhSFW06aJ2Lo1XOZG_re84xGgqre-H4dC0XZR6IQMEAQ9Q5dOXBh8V1NxyLSh0PzyrRRmOnEndoaY4uaIFtbp9j7KnXxQ3ZdGmaYAO96xuhHfO1DbgRdw6fYyf4nnC795yhnwDh1QZGxPsFaysJSA_3-cjmw-29m-Ga0hD1ALfVE7R57iNLxkB6dyEuz1UFJhJyibRDW9sNspo2gQFZZGxMKQ
然后我得到的反应仍然在小提琴手:
您可以按照以下链接获取更多信息,但它与vnext无关。 我将创建一个帖子,因为我需要一个angularJS应用程序来验证和使用隐式流而不是资源所有者流...使用visual studio 2015 preview
我不确定UseOAuthAuthorizationServer的位置,但对于UseOAuthBearerAuthentication ,尝试添加Microsoft.AspNet.Security.OAuthBearer NuGet包,然后在您的启动配置方法中添加:
app.UseOAuthBearerAuthentication(options =>
{
options.Audience = {your audience};
options.Authority = {your authority}}); //or whatever options you need
这里真正的踢球者是代币生成。 我设法使用默认的Microsoft.AspNet.Security.OAuthBearer
包来构建一个包,但这并不容易。
// Injected from the constructor; this is why we configured the options above rather
// than simply passing them to the UseOAuthBearerAuthentication()
private readonly OAuthBearerAuthenticationOptions bearerOptions;
// In your /Token action...
var handler = bearerOptions.SecurityTokenValidators.OfType<System.IdentityModel.Tokens.JwtSecurityTokenHandler>()
.First();
// The identity here is the ClaimsIdentity you want to authenticate the user as.
// You can get this using the SignInManager if you're using Identity.
var securityToken = handler.CreateToken(
issuer: bearerOptions.TokenValidationParameters.ValidIssuer,
audience: bearerOptions.TokenValidationParameters.ValidAudience,
subject: identity);
var token = handler.WriteToken(securityToken);
// The var token is your bearer token
我的完整解决方案详述如下:ASP.Net 5(vNext)中基于令牌的身份验证。
链接地址: http://www.djcxy.com/p/22333.html