问题 微服务架构中的ASP.NET身份


我试图通过将主要组件分解为单独的Web服务器来使用微服务架构来实现Web应用程序。我正在使用ASP.NET Identity(仅限电子邮件/用户名登录,没有Facebook等)和“主”应用程序服务器来实现身份验证服务器。

我目前的挑战是弄清楚如果用户通过身份验证服务器登录,应用程序服务器将如何识别。由于身份验证服务器生成用户验证用户身份的令牌,我想它们存储在某个地方并且可以由应用程序服务器查询,但我不确定如何执行此操作。理想情况下,我的应用程序服务器WebAPI端点将能够使用[Authorize]注释。

问:一个服务器如何使用ASP.NET标识通过单独的身份验证服务器控制访问? 


1574
2017-10-11 00:20


起源



答案:


我通过执行以下操作(使用cookie身份验证)完成了类似的操作:

1 - 将cookie域设置为所有网站的TLD

我的 Startup.Auth.cs 看起来像这样:

app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => {
                        var identity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                        //some additional claims and stuff specific to my needs
                        return Task.FromResult(identity);
                    })
            },
            CookieDomain = ".example.com"
        });

2 - 更新所有网站的web.config以使用相同的 <machineKey />

我看起来像这样:

<machineKey 
    decryption="Auto" 
    decryptionKey="my_key" 
    validation="HMACSHA512"
    validationKey="my_other_key" />

现在,我可以执行登录操作,比方说, account.example.com,并将用户重定向到 site1.example.com 他们将被视为经过认证。


10
2017-10-11 04:12



由于api是无状态的,它不会寻找cookie,不是吗?据我所知,它只查找身份验证标头。这如何适用于API?我打算将API保留在我的MVC项目中,所以api将在example.com/api上,这也可以吗? - CularBytes
所以你可以使用bearer令牌代替。关键是机器上的机器密钥是相同的,否则令牌无法解密。 - Brendan Green
使用身份验证cookie并不一定会使您的API“状态良好”。如果你想拥有 无国籍的行动 (就像在REST中一样),使用身份验证cookie与使用其他身份验证标头相同。您可以安全地使用cookie(它只是一个标准的HTTP标头)。 - SandRock


答案:


我通过执行以下操作(使用cookie身份验证)完成了类似的操作:

1 - 将cookie域设置为所有网站的TLD

我的 Startup.Auth.cs 看起来像这样:

app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => {
                        var identity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

                        //some additional claims and stuff specific to my needs
                        return Task.FromResult(identity);
                    })
            },
            CookieDomain = ".example.com"
        });

2 - 更新所有网站的web.config以使用相同的 <machineKey />

我看起来像这样:

<machineKey 
    decryption="Auto" 
    decryptionKey="my_key" 
    validation="HMACSHA512"
    validationKey="my_other_key" />

现在,我可以执行登录操作,比方说, account.example.com,并将用户重定向到 site1.example.com 他们将被视为经过认证。


10
2017-10-11 04:12



由于api是无状态的,它不会寻找cookie,不是吗?据我所知,它只查找身份验证标头。这如何适用于API?我打算将API保留在我的MVC项目中,所以api将在example.com/api上,这也可以吗? - CularBytes
所以你可以使用bearer令牌代替。关键是机器上的机器密钥是相同的,否则令牌无法解密。 - Brendan Green
使用身份验证cookie并不一定会使您的API“状态良好”。如果你想拥有 无国籍的行动 (就像在REST中一样),使用身份验证cookie与使用其他身份验证标头相同。您可以安全地使用cookie(它只是一个标准的HTTP标头)。 - SandRock