C# 使用关联登录oidc页面直接访问错误-如何重定向?

C# 使用关联登录oidc页面直接访问错误-如何重定向?,c#,.net-core,openid-connect,C#,.net Core,Openid Connect,通过使用AddOpenIdConnect设置asp.net核心,它通过defalt/signin oidc创建一个页面,当从opeind提供程序访问该页面时,该页面工作正常。 用户已登录,一切正常 尽管用户仍然可以尝试直接访问mypage.com/signin oidc,并获得结果关联失败失败错误 如何正确处理对该页面的访问,使其仍然适用于openid流,但在直接访问时不会产生错误(重定向)?(已尝试使用HttpGet覆盖路由) 编辑 更详细地说,转到/signin oidc将给出500个基本启

通过使用
AddOpenIdConnect
设置asp.net核心,它通过defalt
/signin oidc
创建一个页面,当从opeind提供程序访问该页面时,该页面工作正常。 用户已登录,一切正常

尽管用户仍然可以尝试直接访问
mypage.com/signin oidc
,并获得结果
关联失败
失败错误

如何正确处理对该页面的访问,使其仍然适用于openid流,但在直接访问时不会产生错误(重定向)?(已尝试使用HttpGet覆盖路由)

编辑 更详细地说,转到
/signin oidc
将给出500个基本启动状态,如

```

public void配置服务(IServiceCollection服务)
{
services.AddOptions();
services.AddAuthentication(选项=>
{
options.DefaultScheme=CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme=OpenIdConnectDefaults.AuthenticationScheme;
options.defaultsignnscheme=CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie()
.AddOpenIdConnect(选项=>
{
options.ClientId=“测试”;
options.ClientSecret=Environment.GetEnvironmentVariable(“ClientSecret”);
options.signnscheme=CookieAuthenticationDefaults.AuthenticationScheme;
选项。权限=”https://test.net";
options.ResponseType=“code”;
options.Scope.Add(“openid”);
options.GetClaimsFromUserInfoEndpoint=true;
options.SaveTokens=true;
options.Events=新的OpenIdConnectEvents
{
OnTokenValidated=异步ctx=>
{
var索赔=新列表();
添加(新声明(“jwt”,ctx.SecurityToken.ToString());
var appIdentity=新的索赔实体(索赔);
ctx.委托人.额外性(appIdentity);
}
};
}).AddJwtBearer(选项=>
{
选项。权限=”https://test.net";
options.acquisition=“authorization.sample.api”;
options.IncludeErrorDetails=true;
});
services.AddMvc();
services.AddSwaggerGen(c=>
{
c、 大摇大摆的文件(“v1”,新信息
{
Version=“v1”,
Title=“测试API”
});
});
}
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
app.UseStaticFiles(新的StaticFileOptions
{
FileProvider=新的物理FileProvider(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory,“wwwroot”),
RequestPath=“/dist”
});
app.UseForwardedHeaders(新ForwardedHeaders选项
{
ForwardedHeaders=ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
应用程序使用(异步(上下文,下一步)=>
{
if(context.Request.Host.Host.ToLower()!=“localhost”)
context.Request.Scheme=“https”;
等待next.Invoke();
});
app.UseAuthentication();
app.UseMvc(路由=>
{
MapRoute(“默认值”,“{controller=Home}/{action=LandingPage}/{id?}”);
MapRoute(“Spa”和“{*url}”,默认值:new{controller=“Home”,action=“Index”});
});
var swaggerjsonedpoint=“api docs/{0}/swagger.json”;
app.UseSwagger(so=>so.RouteTemplate=string.Format(CultureInfo.InvariantCulture,swaggerJsonEndpoint,“{documentName}”);
app.UseSwaggerUI(c=>
{
c、 RoutePrefix=“api文档”;
c、 SwaggerEndpoint(“/”+string.Format(CultureInfo.InvariantCulture,swaggerJsonEndpoint,“v1”),“TestAPI v1”);
c、 OAuthClientId(“admin.implicit”);
});
}

```

我以前也遇到过这种情况,我认为这只是OpenId系统在ASP.NET内核中工作的一个假象。我相信这是Github的问题,但我似乎找不到它。如果我能找到的话,我会到处看看并把它寄出去

在任何情况下,我都可以通过在OpenId选项事件中添加一个事件来修复此问题,该事件在发生任何远程故障时仅重定向到“主页”:

options.Events = new OpenIdConnectEvents {
    // Your events here
    OnRemoteFailure = ctx => {
        ctx.HandleResponse();
        ctx.Response.Redirect("Home");
        return Task.FromResult(0);
    }
};
看看这对你是否有效


编辑:这是一个问题和建议修复的评论,供您参考

您能分享Startup.cs代码吗?是否正在运行多个节点?已编辑。但我认为没有什么有趣或新鲜的东西是特蕾加,非常感谢你!当用户单击Word文档中的链接时,我们遇到了这种情况。在浏览器中键入相同的URL效果很好。
options.Events = new OpenIdConnectEvents {
    // Your events here
    OnRemoteFailure = ctx => {
        ctx.HandleResponse();
        ctx.Response.Redirect("Home");
        return Task.FromResult(0);
    }
};