【发布时间】:2021-12-14 17:17:23
【问题描述】:
我有一个 Umbraco 9 .Net 5 AspNet Core 项目。
我正在尝试设置身份验证 cookie。我遵循了微软的指南并让它在一个单独的项目中工作,但是当我试图在我的 Umbraco 项目中实现它时它失败了。我不知道为什么,但我猜 Umbraco 9 配置也参与其中。
我已经在登录时在同一个控制器中获取 User.Identity.IsAuthenticated = true,但是一旦我重定向到另一个控制器,身份验证状态为 false。
我也尝试在配置 cookie 时设置 LoginPath 选项,但它仍然重定向到默认路径 (/Account/Login),所以这里的某些东西也不起作用
我的 StartUp.cs 如下所示
public void ConfigureServices(IServiceCollection services)
{
services.AddUmbraco(mEnvironment, mConfig)
.AddBackOffice()
.AddWebsite()
.AddComposers()
.Build();
services.AddDistributedMemoryCache();
//services.AddSession(options =>
//{
// options.IdleTimeout = TimeSpan.FromSeconds(10);
// options.Cookie.HttpOnly = true;
// options.Cookie.IsEssential = true;
//});
services.AddControllersWithViews();
services.AddRazorPages();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
{
options.LoginPath = "/portal/"; //not working, still redirects to default
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
app.UseAuthorization();
//umbraco setup
app.UseUmbraco()
.WithMiddleware(u =>
{
u.UseBackOffice();
u.UseWebsite();
})
.WithEndpoints(u =>
{
u.UseInstallerEndpoints();
u.UseBackOfficeEndpoints();
u.UseWebsiteEndpoints();
});
//app.UseSession();
}
我的登录控制器操作如下所示:
public async Task<ActionResult> Login()
{
var claimsIdentity = new ClaimsIdentity(new List<Claim>
{
new Claim(UserClaimProperties.UserRole, MemberRole, ClaimValueTypes.String)
}, CookieAuthenticationDefaults.AuthenticationScheme);
var authProps = new AuthenticationProperties
{
ExpiresUtc = DateTime.UtcNow.AddMinutes(20),
IsPersistent = true,
AllowRefresh = true,
RedirectUri = "/"
};
await HttpContext.SignInAsync(
//CookieAuthenticationDefaults.AuthenticationScheme, //from MS-example but isAuth will be false using this
new ClaimsPrincipal(claimsIdentity),
authProps);
var isAuthenticated = User.Identity.IsAuthenticated;
return Redirect("/myview/");
}
如果我在 SignInAsync 中将 Auth Scheme 设置为“Cookies”,就像在 Microsoft 示例中一样,isAuthenticated 将是错误的,但如果没有这个,我至少会在这里得到它。
当重定向到下一个操作时,User.Identity.IsAuthenticated 为 false。
有什么建议或者为什么我的 LoginPath 配置不起作用?
编辑:我不想为每个登录的用户创建 Umbraco 成员。我只想让用户登录到上下文并能够验证用户是否已登录我自己在我的控制器中。
编辑 2: 我尝试捕捉登录事件并在其中找到了一个断点。在我的演示应用程序(没有 umbraco)中,我将到达带有 Umbraco 的断点,这个断点永远不会被击中。这是因为 Umbraco 可能会覆盖此事件或劫持事件吗?
【问题讨论】:
标签: authentication .net-core cookies umbraco