我有一個 Umbraco 9 .Net 5 AspNet Core 專案。
我正在嘗試設定一個身份驗證 cookie。我遵循了 microsofts 指南并讓它在一個單獨的專案中作業,但是當試圖在我的 Umbraco 專案中實作它時它失敗了。我不知道為什么,但我猜 Umbraco 9 配置有一部分。
我已經在我登錄的同一個控制器中獲得 User.Identity.IsAuthenticated = true ,但是一旦我重定向到另一個控制器,身份驗證狀態就為假。
我也嘗試在配置 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/");
}
If I set the Auth Scheme to "Cookies" in SignInAsync like it is in the microsoft example isAuthenticated will be false but without this I'll at least get it true here.
When redirected to the next action the User.Identity.IsAuthenticated is false.
Any suggestions why that is or why my LoginPath configuration wont work?
Edit: I don't want to create Umbraco members for each user that logs in. I just want to sign in a user to the context and be able to validate that the user is signed in by myself in my controllers.
Edit 2: I've try to catch the sign in event and got a breakpoint in that even. In my demo app(without umbraco) I'll get to the breakpoint in the one with Umbraco this breakpoint is never hit so. Is this because Umbraco probably override this or hijack the event?
uj5u.com熱心網友回復:
您是否有機會閱讀 Scott Brady 的關于“使用 OpenID Connect 的 Umbraco 前端會員 SSO”的文章?
我已經按照 Scott 的方法成功地讓會員登錄到我的 Umbraco v9 網站,這種方法也可能對您有所幫助。
他還為“Umbraco backoffice SSO with OpenID Connect”以及其他一些非常有用的 .NET Core 特定文章撰寫了一篇文章,這些文章可能對您有所幫助。
uj5u.com熱心網友回復:
不知道為什么,但在測驗了不同的身份驗證方案后,我收到一個錯誤,即我測驗的方案未注冊,并且我得到了已注冊方案的串列。我認為通過這樣做
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
{
options.LoginPath = "/portal/"; //not working, still redirects to default
});
我已經注冊了“Cookies”計劃。
列為注冊的方案之一是“Identity.Application”,通過使用該方案,我可以從重定向控制器的背景關系中獲取用戶身份。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/381735.html
標籤:authentication .net核心 饼干 翁布拉科
上一篇:顫振-supabase密碼恢復
