更新:它看起来像下面的代码失败:
services.AddDataProtection()
.SetApplicationName(appname)
.PersistKeysToRedis(redis, "DataProtectionKeys")
.ProtectKeysWithCertificate(LoadCert(Configuration));
这是无法从PFX文件中读取证书。
UPDATE2:噢,我的!证书文件已被排除的.gitignore!:))生活与学习。至少我们住吧;!?))
最初的问题:我有ASP.NET 2.1的核心应用程序部署在背后泊坞容器AWS负载平衡器。当我试图登录到从登录页面的应用程序。我得到InvalidOperationException异常与此理由:
没有authenticationScheme指定,也没有发现DefaultChallengeScheme。
但是,当我再次命中同一个URL,它实际上移动到正确的页面和工作了一段时间,然后再抛出,HTTP状态500和第2次尝试打开它成功相同的页面后相同的异常。有趣的是Chrome是不一样强大IE:如果IE无法异常后恢复浏览器总是在随后提交的页面,产生上述异常返回404。
所以,我将不胜感激,如果有人将能够提供给我的想法如何纠正这种情况很明显的问题是相关的认证,但我无法弄清楚到底应该做什么。
下面是从ConfigureServices()在Startup.cs相关使出:
string appname = "MyApp";
var redis = ConnectionMultiplexer.Connect(Configuration.GetConnectionString("RedisConnection"));
services.AddDataProtection()
.SetApplicationName(appname)
.PersistKeysToRedis(redis, "DataProtectionKeys")
.ProtectKeysWithCertificate(LoadCert(Configuration));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddAuthentication( CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
options =>
{
options.LoginPath = new PathString("/Area/Ctrl/Login");
options.LogoutPath = new PathString("/Area/Ctrl/Logout");
options.Cookie.IsEssential = true;
});
services.AddDistributedRedisCache(o =>
{
o.Configuration = Configuration.GetConnectionString("RedisConnection");
});
services.AddSession(options =>
{
options.Cookie.Name = appname;
options.IdleTimeout = TimeSpan.FromSeconds(600);
});
下面是从Startup.cs配置()相关的代码:
app.UseForwardedHeaders();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication();
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areas",
template: "{area:exists}/{controller=Ctrl}/{action=Login}/{id?}"
);
});
这里是我如何设置在主控制器,它处理登录:
ClaimsIdentity identity = new ClaimsIdentity(GetUserRoleClaims(dbUserData), CookieAuthenticationDefaults.AuthenticationScheme);
ClaimsPrincipal principal = new ClaimsPrincipal(identity);
if (principal == null)
throw new ApplicationException($"Could not create principal for {user?.UserName} user.");
await httpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
if (httpContext.User == null)
{
httpContext.User = principal;
}
OK,一切现在工作:)这是很大的影响:
ProtectKeysWithCertificate()
调用应该正确加载。如果无法加载不打这通电话可言,但是这将是非常糟糕的主意。只要弄清楚为什么它不加载。有关证书一件重要的事情:数据保护模块只支持与CAPI私钥证书。 CNG的人被留了下来。