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页面.

推荐答案

编辑(2021年1月28日):AspNet.安全OpenIdConnect.作为3.0更新的一部分,服务器已合并为OpenIddict.要开始使用OpenIddict,请访问documentation.openiddict.com.


好的,让我们回顾一下OWIN/Katana 3提供的不同OAuth2中间件(以及它们各自的IAppBuilder个扩展),以及将被移植到ASP.NET Core的中间件:

  • app.UseOAuthBearerAuthentication/OAuthBearerAuthenticationMiddleware:它的名字并不明显,但它曾经(现在仍然是,因为它已经被移植到ASP.NET内核中)负责验证OAuth2服务器中间件发布的访问令牌.它基本上是cookies中间件的token counterpart,用于保护您的API.In ASP.NET Core, it has been enriched with optional OpenID Connect features(它现在能够从颁发令牌的OpenID Connect服务器自动检索签名证书).

Note: starting with ASP.NET Core beta8, it is now named100/101.

  • app.UseOAuthAuthorizationServer/OAuthAuthorizationServerMiddleware:顾名思义,OAuthAuthorizationServerMiddleware是一个OAuth2授权服务器中间件,用于创建和发布访问令牌.This middleware won't be ported to ASP.NET Core:OAuth Authorization Service in ASP.NET Core.

  • app.UseOAuthBearerTokens:这个扩展实际上并不对应于中间件,只是app.UseOAuthAuthorizationServerapp.UseOAuthBearerAuthentication的包装器.它是ASP的一部分.NET Identity package,它只是配置OAuth2授权服务器和用于在单个调用中验证访问令牌的OAuth2承载中间件的一种方便方法.It won't be ported to ASP.NET Core

ASP.NET Core将提供一个全新的中间件(我很自豪地说是我设计的):

  • app.UseOAuthAuthentication/OAuthAuthenticationMiddleware:这个新的中间件是一个通用的OAuth2交互客户端,其行为与app.UseFacebookAuthenticationapp.UseGoogleAuthentication完全相同,但实际上支持任何标准的OAuth2提供者,包括您的.谷歌、Facebook和微软的供应商都已经更新,以继承这个新的基础中间件.

因此,您实际上要寻找的中间件是OAuth2 authorization server middleware,也就是OAuthAuthorizationServerMiddleware.

Though it is considered as an essential component by a large part of the community, it won't be ported to ASP.NET Core美元.

幸运的是,已经有了一个直接替代者:AspNet.Security.OpenIdConnect.Server(https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server)

该中间件是OAuth2授权服务器中间件的高级分支,它附带了Katana 3,但目标是OpenID Connect(它本身基于OAuth2).它使用相同的低级方法,提供细粒度的控制(通过各种通知),并允许您使用自己的框架(Nancy,ASP.NET Core MVC)为授权页面提供服务,就像使用OAuth2服务器中间件一样.配置它很容易:

ASP.NET Core 1.x:

// Add a new middleware validating access tokens issued by the server.
app.UseOAuthValidation();

// Add a new middleware issuing tokens.
app.UseOpenIdConnectServer(options =>
{
    options.TokenEndpointPath = "/connect/token";

    // Create your own `OpenIdConnectServerProvider` and override
    // ValidateTokenRequest/HandleTokenRequest to support the resource
    // owner password flow exactly like you did with the OAuth2 middleware.
    options.Provider = new AuthorizationProvider();
});

ASP.NET Core 2.x:

// Add a new middleware validating access tokens issued by the server.
services.AddAuthentication()
    .AddOAuthValidation()

    // Add a new middleware issuing tokens.
    .AddOpenIdConnectServer(options =>
    {
        options.TokenEndpointPath = "/connect/token";

        // Create your own `OpenIdConnectServerProvider` and override
        // ValidateTokenRequest/HandleTokenRequest to support the resource
        // owner password flow exactly like you did with the OAuth2 middleware.
        options.Provider = new AuthorizationProvider();
    });

有一个OWIN/Katana 3版本和一个ASP.NET Core版本都支持.NET桌面和.净核心.

不要犹豫,试着go 理解它是如何工作的.I'd recommend reading 101, that explains how you can implement the resource owner password flow.

如果你还需要帮助,请随时打电话给我.

Asp.net相关问答推荐

SignalR 不在服务器上使用 Session

无法获取项目引用的依赖项

如何从以特定名称开头的 appsettings 键中获取所有值并将其传递给任何数组?

如何判断一个IP地址是否是私有的?

多个 DataContext 类是否合适?

ASP.net 页面在导入语句上出现错误,但我确实有参考?

用于链接字符串中的 url 的 C# 代码

RestSharp 不反序列化 JSON 对象列表,始终为 Null

System.Web.HttpContext 无法识别

没有回发的按钮?

如何使用 ASP.Net MVC 路由来路由图像?

以编程方式从后面的代码中关闭 aspx 页面

在asp.net mvc 3中实现FilterAttribute,IActionFilter和从ActionFilterAttribute继承有什么区别?

在 ASP.NET 中实现 404 的最佳方法

GMT 和 UTC 一样吗?

ASP.NET Core 中的 NuGet 包位置在哪里?

VS2017 无法加载文件或程序集 Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll 或其依赖项之一

ASP.NET MVC 4 的 Windows 身份验证 - 它是如何工作的,如何测试它

当文件上传超出 ASP.NET MVC 中允许的大小时显示自定义错误页面

根据条件更改 GridView 行 colored颜色