【发布时间】:2021-06-28 19:25:10
【问题描述】:
我在同一个域上运行一个 asp.net Framework 4.8 webforms 项目和一个 asp.net core 3.1 项目,每个项目都有单独的子域。
我希望能够让用户在新站点上使用 .net 核心身份登录,这将为他们分配一个 cookie,并让该 cookie 使用 Owin 中间件在旧的 webforms 应用程序上对他们进行身份验证。
从我所做的研究看来,使用 webforms 身份验证和 .net 核心似乎无法做到这一点。但是,是否可以使用 microsoft.aspnet.identity 和 Owin 中间件?我能否以某种方式使用 Owin 中间件解密 webforms 站点上的 .net 核心 cookie,并使用它为 webforms 站点设置 aspnet.identity cookie 的声明,或者只是将这些声明注入旧应用程序,这样我就可以拥有我的 HttpContext 用户身份想过这个应用程序吗?
据说John Geddes 找到了解决方案,但没有时间分享他所做的事情(请参阅他对此answer 的评论)。他说他在 .net framework webforms 应用上使用了 Owin,但没有提供更多细节。
这是我的 .net core 启动类
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<User, IdentityRole>(cfg =>
{
cfg.User.RequireUniqueEmail = true;
})
.AddEntityFrameworkStores<SJRContext>()
.AddSignInManager<SJRSignInManager<User>>()
.AddDefaultTokenProviders()
.AddTokenProvider<CustomTwoFactorTokenProvider<User>>("TwoFactorAuthToken");
//cookie is added here
services.AddAuthentication()
.AddCookie(cfg =>
{
cfg.ExpireTimeSpan = TimeSpan.FromMinutes(20);
cfg.LoginPath = @"/Account/Login";
cfg.LogoutPath = @"/Account/Logout";
cfg.SlidingExpiration = true;
cfg.Cookie.Expiration = TimeSpan.FromMinutes(30);
cfg.Cookie.Domain = Configuration.GetSection("AppDomain").Value;
});
//More service set up here...
services.AddSession();
}
public void OnShutdown()
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//More middleware here...
app.Use(async (ctx, next) =>
{
//Get and set cookie exp date according to ip address and client device here
//Go to next middleware
await next();
});
//More middleware here....
app.UseAuthorization();
}
}
这是在asp.net framework 4.8 webforms上设置cookie的Web.config
<system.web>
<authentication mode="Forms">
<!-- Set login to login page for development -->
<forms loginUrl="~/Account/Login.aspx" name=".AspNetCore.Identity.Application" enableCrossAppRedirects="true" cookieless="UseCookies" requireSSL="false" timeout="60" slidingExpiration="true" />
</authentication>
<authorization>
<deny users="?" />
<allow users="*" />
</authorization>
</system.web>
这是我的 web.release.config 文件,用于在发布模式下覆盖上述文件
<system.web>
<authentication mode="Forms">
<!-- Set login to .net core login page for production (Release)-->
<forms loginUrl="https://MyDotNetCoreSubDomain.SharedDomain.com/Account/Login" enableCrossAppRedirects="true" name=".AspNetCore.Identity.Application" cookieless="UseCookies" requireSSL="false" timeout="4320" domain=".SharedDomain.com" slidingExpiration="true" xdt:Transform="Replace"/>
</authentication>
</system.web>
【问题讨论】:
-
嗨@BoMerican,正如答案所说,在网络表单和asp.net核心之间共享cookie是不可能的。
-
Rena,是的,但是如果您查看 @JohnGeddes 对 answer 的评论,他最终会弄清楚如何使用 Owin 中间件。这就是我现在想要弄清楚的。我想慢慢升级我的旧网络表单应用,而不是强迫用户分别登录这两个应用。
-
@Rena 我更新了我的问题,以便更清楚地了解我想要去的方向。
标签: asp.net .net asp.net-core cookies webforms