gpt4 book ai didi

asp.net-mvc - CookieAuthenticationOptions.LoginPath 值在也使用 app.UseOpenIdConnectAuthentication 时不使用

转载 作者:行者123 更新时间:2023-12-01 02:30:16 24 4
gpt4 key购买 nike

我正在使用 OWIN 中间件进行 cookie 身份验证和 openIdConnect。在我将 openIdConnect 身份验证添加到我的启动身份验证代码和 cookie 身份验证选项之前,LoginPath 被用作重定向未经身份验证的用户的目标。这非常有效,也是我想保留的功能。

但是,当我将 app.UseOpenIdConnectAuthentication 添加到我的项目时,它开始自动将未经身份验证的用户重定向到我的 OpenIdConnect 机构 ( https://login.windows.net/ )。

有没有办法可以禁用 OpenIdConnectAuthentication 为未经身份验证的用户设置重定向路径并依赖为 cookie 身份验证设置的 LoginPath?我当前的解决方法是在我的授权属性中手动设置重定向路径,但如果可能,我希望让 OWIN 中间件处理此问题。

谢谢。

代码:

public void ConfigureAuth(IAppBuilder app)
{
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
var cookieOptions = new CookieAuthenticationOptions();
cookieOptions.AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie;
cookieOptions.LoginPath = new PathString("/Account/Login");

app.SetDefaultSignInAsAuthenticationType(cookieOptions.AuthenticationType);

app.UseCookieAuthentication(cookieOptions);

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
AuthenticationType = FranchiseAuthType,
ClientId = franchiseClientId,
Authority = FranchiseAuthority,
PostLogoutRedirectUri = postLogoutRedirectUri,
});
}

最佳答案

我不确定你是否能够解决这个问题,但你想要做的是添加

AuthenticationMode = AuthenticationMode.Passive

到您的身份验证选项。这将使 OpenIdConnect 身份验证仅依赖您的代码来调用它。我相信这就是你打算发生的事情。

所以你的新代码应该是这样的:
public void ConfigureAuth(IAppBuilder app)
{
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
var cookieOptions = new CookieAuthenticationOptions();
cookieOptions.AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie;
cookieOptions.LoginPath = new PathString("/Account/Login");

app.SetDefaultSignInAsAuthenticationType(cookieOptions.AuthenticationType);

app.UseCookieAuthentication(cookieOptions);

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
AuthenticationType = FranchiseAuthType,
AuthenticationMode = AuthenticationMode.Passive,
ClientId = franchiseClientId,
Authority = FranchiseAuthority,
PostLogoutRedirectUri = postLogoutRedirectUri,
});
}

关于asp.net-mvc - CookieAuthenticationOptions.LoginPath 值在也使用 app.UseOpenIdConnectAuthentication 时不使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29685857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com