pin*_*nki 5 asp.net authentication cookies single-sign-on asp.net-core
我有两个ASP.NET 5 MVC 6应用程序.
一种是在运行www.mydomain.tld
,一个在world1.mydomain.tld
.
如果用户登录www子域的应用程序,我希望她也登录到world1子域的应用程序.登录是使用ASP.NET Identity 3实现的.
我已将这两个应用程序设置Startup.cs
如下:
public void ConfigureServices (IServiceCollection services) {
// [...]
services.AddCaching();
services.AddSession(
options => {
options.CookieDomain = ".mydomain.tld";
options.IdleTimeout = TimeSpan.FromMinutes(30);
}
);
// [...]
}
public void Configure (IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory) {
// [...]
app.UseCookieAuthentication(null, IdentityOptions.ExternalCookieAuthenticationScheme);
app.UseCookieAuthentication(null, IdentityOptions.TwoFactorRememberMeCookieAuthenticationScheme);
app.UseCookieAuthentication(null, IdentityOptions.TwoFactorUserIdCookieAuthenticationScheme);
app.UseCookieAuthentication(
config => {
config.CookieDomain = ".mydomain.tld";
},
IdentityOptions.ApplicationCookieAuthenticationScheme
);
// [...]
}
Run Code Online (Sandbox Code Playgroud)
我还通过web.config
以下方式设置了两个应用程序的机器密钥:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<machineKey decryption="AES"
decryptionKey="SOME DECRYPTION KEY"
validation="HMACSHA256"
validationKey="SOME ENCRYPTION KEY" />
</system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)
登录www子域可以正常工作,但访问world1子域上的站点不起作用,因为身份验证cookie未被识别为有效的登录cookie.
我究竟做错了什么?
应用程序会自动相互隔离.你需要确保三件事;
在同一主机上运行的应用程序在相同的托管机制下将使用相同的密钥库.如果它们位于不同的计算机上,则需要在网络驱动器或其他共享位置(如azure blob存储)上使用密钥存储区.
要设置两个应用程序通用的应用程序ID,您需要配置数据保护堆栈.
例如,
public void ConfigureServices(IServiceCollection services)
{
services.AddDataProtection();
services.ConfigureDataProtection(configure =>
{
configure.SetApplicationName("my application");
});
}
Run Code Online (Sandbox Code Playgroud)
如果需要以不同用户身份运行应用程序,则需要更改密钥的保护方式,以使用机器级DPAPI或X509证书.
您的web.config中不需要机器密钥条目,机器密钥不再是用户.
归档时间: |
|
查看次数: |
2012 次 |
最近记录: |