Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 微服务体系结构中的ASP.NET身份_C#_Asp.net_Asp.net Web Api_Asp.net Identity_Asp.net Identity 2 - Fatal编程技术网

C# 微服务体系结构中的ASP.NET身份

C# 微服务体系结构中的ASP.NET身份,c#,asp.net,asp.net-web-api,asp.net-identity,asp.net-identity-2,C#,Asp.net,Asp.net Web Api,Asp.net Identity,Asp.net Identity 2,我正试图通过将主要组件分解为单独的web服务器来实现一个使用微服务体系结构的web应用程序。我正在使用ASP.NET身份(仅限电子邮件/用户名登录,无Facebook等)和“主”应用程序服务器实现身份验证服务器 我目前面临的挑战是弄清楚应用程序服务器如何识别用户是否已通过身份验证服务器登录。由于身份验证服务器生成令牌供it用户验证用户的身份,我认为它们存储在某个地方,可以由应用服务器查询,但我不确定如何执行此操作。理想情况下,我的应用服务器WebAPI端点将能够使用[Authorize]注释 Q

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

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


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

我做了以下类似的事情(使用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"
        });
<machineKey 
    decryption="Auto" 
    decryptionKey="my_key" 
    validation="HMACSHA512"
    validationKey="my_other_key" />
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
LoginPath=新路径字符串(“/Account/Login”),
Provider=新CookieAuthenticationProvider
{
OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
validateInterval:TimeSpan.FromMinutes(30),
重新生成标识:(管理者、用户)=>{
var identity=manager.CreateIdentity(用户,DefaultAuthenticationTypes.ApplicationOkie);
//一些额外的索赔和特定于我需要的东西
返回任务.FromResult(标识);
})
},
CookieDomain=“.example.com”
});
2-更新所有网站的web.config以使用相同的

我的看起来像这样:

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"
        });
<machineKey 
    decryption="Auto" 
    decryptionKey="my_key" 
    validation="HMACSHA512"
    validationKey="my_other_key" />


现在我可以在,
account.example.com
上执行登录操作,并将用户重定向到
site1.example.com
,它们将被视为经过身份验证。

由于api是无状态的,它不会查找cookies,是吗?据我所知,它只查找身份验证标头。对于API,这将如何工作?我计划将API保留在我的MVC项目中,因此API将位于example.com/API,这也可以吗?因此您可以使用承载令牌。关键是机器密钥在不同的机器上是相同的,否则令牌不能被解密。使用身份验证cookie不一定会使API“statefull”。如果您尝试进行无状态操作(如在REST中),则使用身份验证cookie与使用另一个身份验证标头完全相同。您可以安全地使用cookie(它只是一个标准HTTP头)。