[英]Using Identity Server for Authentication with ASP.NET Web App and .NET 6
我們公司有定制的身份服務器,我們的一些 web 應用程序使用它進行身份驗證。 我正在嘗試將我們的身份服務器與新創建的 ASP.NET 核心 Web 應用程序一起使用,使用 .NET 6 框架。 我正在嘗試使用預定義的 OIDC URL,而不必自己編寫代碼。
身份驗證主要是有效的; 比如我給某個Razor的PageModel添加[Authorize],它會自動重定向到Authority URL,認證成功后返回那個頁面登錄。
我遇到的問題是:我似乎無法讓自動注銷工作。 我正在嘗試使用任一預定義的 OIDC 注銷 URL(signout-oidc 或 signout-callback-oidc),但我似乎遺漏了一些東西。 我也很難找到好的示例代碼或清晰的文檔來幫助調試問題。
我也嘗試過使用 OIDC 事件——例如“OnSignedOutCallbackRedirect”:
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", async options =>
{
options.Authority = testIdentitySettings.Authority;
options.SignedOutRedirectUri = testIdentitySettings.SignedOutRedirectUri;
options.RequireHttpsMetadata = testIdentitySettings.RequireHttpsMetadata ?? true;
options.ClientId = testIdentitySettings.ClientId;
options.SignInScheme = "Cookies";
options.Scope.Add("roles");
options.SaveTokens = true;
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
RoleClaimType = "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"
};
options.Events.OnSignedOutCallbackRedirect = async (context) =>
{
await context.HttpContext.SignOutAsync("Cookies");
var redirUrl = context.Options.SignedOutRedirectUri;
var prop = new AuthenticationProperties
{
RedirectUri = redirUrl
};
await context.HttpContext.SignOutAsync("oidc", prop);
context.Response.Redirect(redirUrl);
context.HandleResponse();
};
});
這幾乎似乎工作。 它確實重定向到我的 SignedOutRedirectUri (/LoggedOut),當我檢查該頁面上的用戶時,User.Identity 顯示 IsAuthenticated = false,並且聲明為零; 但是,如果我隨后加載主頁 (/),則 User.Identity 將恢復為已通過所有聲明進行身份驗證。
任何幫助或見解將不勝感激。
對於注銷示例,您可以參考下面的代碼。
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Auth0.AspNetCore.Authentication;
public class AccountController : Controller
{
[Authorize]
public async Task Logout()
{
var authenticationProperties = new LogoutAuthenticationPropertiesBuilder()
// Indicate here where Auth0 should redirect the user after a logout.
// Note that the resulting absolute Uri must be added to the
// **Allowed Logout URLs** settings for the app.
.WithRedirectUri(Url.Action("Index", "Home"))
.Build();
await HttpContext.SignOutAsync(Auth0Constants.AuthenticationScheme, authenticationProperties);
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}
}
參考: 注銷
如果問題仍然存在,您可以嘗試使用下面的代碼示例進行測試,以減少AddCookie
配置中的ExpireTimeSpan
。
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(/* ... */)
.AddCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromMinutes(1);
});
// ...
}
有關更多詳細信息,請參閱此答案。
我會用它來注銷:
[Authorize]
/// <summary>
/// Do the actual logout
/// </summary>
/// <returns></returns>
public async Task DoLogout()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
}
我認為您不需要使用 OnSignedOutCallbackRedirect 處理程序。
在您的情況下,由於您已重命名方案,注銷方法應為:
[Authorize]
/// <summary>
/// Do the actual logout
/// </summary>
/// <returns></returns>
public async Task DoLogout()
{
await HttpContext.SignOutAsync("Cookies");
await HttpContext.SignOutAsync("oidc");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.