Question
我们如何在ASP中使用承载令牌.NET 5使用用户名和密码流?对于我们的场景,我们希望让用户使用AJAX调用注册和登录,而无需使用外部登录.
为此,我们需要一个授权服务器端点.In the previous versions of ASP.NET我们将执行以下操作,然后在ourdomain.com/Token
URL处登录.
// Configure the application for OAuth based flow
PublicClientId = "self";
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Token"),
Provider = new ApplicationOAuthProvider(PublicClientId),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14)
};
在当前版本的ASP.不过,上述方法并不奏效.我们一直在试图找出新的方法.例如,GitHub上的aspnet/identity example配置了Facebook、Google和Twitter身份验证,但似乎没有配置非外部OAuth授权服务器端点,除非AddDefaultTokenProviders()
是这样做的,在这种情况下,我们想知道该Provider 的URL是什么.
Research
我们从reading the source here中了解到,我们可以通过调用Startup
类中的IAppBuilder.UseOAuthBearerAuthentication
将"承载身份验证中间件"添加到HTTP管道中.这是一个良好的开端,尽管我们仍然不确定如何设置其令牌端点.这不管用:
public void Configure(IApplicationBuilder app)
{
app.UseOAuthBearerAuthentication(options =>
{
options.MetadataAddress = "meta";
});
// if this isn't here, we just get a 404
app.Run(async context =>
{
await context.Response.WriteAsync("Hello World.");
});
}
一到ourdomain.com/meta
岁,我们就会收到问候世界页面.
进一步的研究表明,我们也可以使用IAppBuilder.UseOAuthAuthentication
扩展方法,它需要OAuthAuthenticationOptions
个参数.该参数具有TokenEndpoint
属性.所以,虽然我们不确定我们在做什么,但我们try 了这个,当然没有成功.
public void Configure(IApplicationBuilder app)
{
app.UseOAuthAuthentication("What is this?", options =>
{
options.TokenEndpoint = "/token";
options.AuthorizationEndpoint = "/oauth";
options.ClientId = "What is this?";
options.ClientSecret = "What is this?";
options.SignInScheme = "What is this?";
options.AutomaticAuthentication = true;
});
// if this isn't here, we just get a 404
app.Run(async context =>
{
await context.Response.WriteAsync("Hello World.");
});
}
换言之,在进入ourdomain.com/token
时,没有错误,只是再次出现了我们的hello world页面.