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

时间:2014-10-11 00:20:31

标签: c# asp.net asp.net-web-api asp.net-identity asp.net-identity-2

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

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

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

1 个答案:

答案 0 :(得分:10)

我通过执行以下操作(使用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,他们将被视为已通过身份验证。

相关问题