【发布时间】:2018-02-17 12:39:35
【问题描述】:
我们在 IIS 中设置了多个应用程序,其中一个应用程序处理所有应用程序的登录。此应用程序是一个 asp.net 4 站点并使用表单身份验证 cookie。
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" protection="All" cookieless="UseCookies" path="/" name="CookieName" />
</authentication>
我们可以通过 owin 成功使用此 cookie 登录 asp.net 4.5 应用程序。
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
TicketDataFormat = new SharedTicketDataFormat(),
CookieName = "CookieName",
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity =
SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
public class SharedTicketDataFormat : ISecureDataFormat<AuthenticationTicket>
{
public string Protect(AuthenticationTicket data)
{
return FormsAuthentication.Encrypt(new FormsAuthenticationTicket(data.Identity.Name, false, -1));
}
public AuthenticationTicket Unprotect(string protectedText)
{
var ticket = FormsAuthentication.Decrypt(protectedText);
var identity = new FormsIdentity(ticket);
return new AuthenticationTicket(identity, new AuthenticationProperties());
}
}
在 asp.net core 2.0 中我不知道要连接应用程序以使用共享 cookie
在 Startup.cs 中 配置
app.UseAuthentication();
配置服务
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
options.Cookie.Name = "CookieName";
});
【问题讨论】:
-
嗨,你能解决这个问题吗?我也在寻找类似的解决方案。
-
@SamJackSon 不,很遗憾没有,由于时间压力,我们回到了 asp.net 4.6。
-
感谢@skyfoot 的回复。我尝试使用此解决方案,并且初步结果很好,似乎可以正常工作。更多地使用 AuthorizationHandler。 github.com/dazinator/AspNetCore.LegacyAuthCookieCompat
标签: asp.net cookies forms-authentication asp.net-core-2.0