适用于多种应用程序的OWIN Authentication Server

Nei*_*ens 3 oauth asp.net-web-api owin asp.net-mvc-5

我正在实现一个具有MVC客户端的解决方案(让我们在localhost:4077 /上调用此CLIENT),其中包含一个WebAPI服务(在localhost上称为API:4078 /)

我在API中实现了OWIN OAuth,但是想知道OWIN是否可以在单独的解决方案中实现(让我们在localhost:4079/token上调用AUTH)来为CLIENT生成令牌,然后CLIENT将其传递给API (作为承载授权令牌)

我查询的原因是,CLIENT可能会访问其他WebAPI服务,我想在客户端和所有API服务之间使用OWIN.

问题是我不确定AUTH服务生成的令牌是否可用于授权CLIENT和所有API服务上的所有请求.

有没有人实现这样的任何东西,如果是这样你能提供一个例子,我是OWIN和OAUTH的新手,所以任何帮助将不胜感激

Kév*_*let 5

将授权服务器与资源服务器分离非常简单:如果使用IIS并且在两个应用程序/服务器上配置了相同的机器密钥,它甚至可以在没有任何额外代码的情况下工作.

如果您需要选择访问令牌可以访问的端点,那么使用OWIN OAuth2服务器支持多个资源服务器会更难实现.如果您不关心这一点,只需使用相同的机器密钥配置所有资源服务器,您就可以使用相同的令牌访问所有API.

为了更好地控制可以与访问令牌一起使用的端点,您应该看一下AspNet.Security.OpenIdConnect.Server- OWIN/Katana附带的OAuth2服务器的分支 - 本机支持这种情况:https://github.com/ aspnet-contrib/AspNet.Security.OpenIdConnect.Server.

它的设置相对容易:

在授权服务器应用程序中添加一个新的中间件签发令牌(in Startup.cs):

app.UseOpenIdConnectServer(new OpenIdConnectServerOptions
{
    Provider = new AuthorizationProvider()
});
Run Code Online (Sandbox Code Playgroud)

添加新的中间件验证不同API服务器(in Startup.cs)中的访问令牌:

app.UseJwtBearerAuthentication(new JwtBearerAuthenticationOptions
{
    // AllowedAudiences MUST contain the absolute URL of your API.
    AllowedAudiences = new[] { "http://localhost:11111/" },

    // X509CertificateSecurityTokenProvider MUST be initialized with an issuer corresponding to the absolute URL of the authorization server.
    IssuerSecurityTokenProviders = new[] { new X509CertificateSecurityTokenProvider("http://localhost:50000/", certificate) }
});

app.UseJwtBearerAuthentication(new JwtBearerAuthenticationOptions
{
    // AllowedAudiences MUST contain the absolute URL of your API.
    AllowedAudiences = new[] { "http://localhost:22222/" },

    // X509CertificateSecurityTokenProvider MUST be initialized with an issuer corresponding to the absolute URL of the authorization server.
    IssuerSecurityTokenProviders = new[] { new X509CertificateSecurityTokenProvider("http://localhost:50000/", certificate) }
});
Run Code Online (Sandbox Code Playgroud)

最后,在您的客户端应用程序中添加一个新的OpenID Connect客户端中间件(in Startup.cs):

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
    // Some essential parameters have been omitted for brevity.
    // See https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/blob/dev/samples/Mvc/Mvc.Client/Startup.cs for more information

    // Authority MUST correspond to the absolute URL of the authorization server.
    Authority = "http://localhost:50000/",

    // Resource represents the different endpoints the
    // access token should be issued for (values must be space-delimited).
    // In this case, the access token will be requested for both APIs.
    Resource = "http://localhost:11111/ http://localhost:22222/",
});
Run Code Online (Sandbox Code Playgroud)

您可以查看此示例以获取更多信息:https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/blob/dev/samples/Mvc/

它不使用多个资源服务器,但使用我提到的不同步骤来调整它应该不难.如果您需要帮助,请随时给我打电话.