Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使ASP.Net核心Web API标识在未经授权的情况下返回401_C#_Authentication_Identityserver4_Asp.net Core Webapi_Asp.net Core Identity - Fatal编程技术网

C# 如何使ASP.Net核心Web API标识在未经授权的情况下返回401

C# 如何使ASP.Net核心Web API标识在未经授权的情况下返回401,c#,authentication,identityserver4,asp.net-core-webapi,asp.net-core-identity,C#,Authentication,Identityserver4,Asp.net Core Webapi,Asp.net Core Identity,我正在尝试使ASP.net核心web api JSON web令牌身份验证正常工作。我已经成功地将IdentityServer4与应用程序集成,并且它成功地处理了基于ASP.net核心身份的登录 但是,每当身份验证失败时,API都会返回302结果,试图将客户端重定向到登录页面。然而,这是一个纯Web API,没有用户页面,也没有用户应该直接与之交互的任何内容 如何让系统返回401,而不是尝试重定向到登录页面 ConfigureServices的标识部分如下所示: // ASP.ne

我正在尝试使ASP.net核心web api JSON web令牌身份验证正常工作。我已经成功地将IdentityServer4与应用程序集成,并且它成功地处理了基于ASP.net核心身份的登录

但是,每当身份验证失败时,API都会返回302结果,试图将客户端重定向到登录页面。然而,这是一个纯Web API,没有用户页面,也没有用户应该直接与之交互的任何内容

如何让系统返回401,而不是尝试重定向到登录页面

ConfigureServices的标识部分如下所示:

       // ASP.net Identity
        var identityBackingStoreConnectionString = configuration["identity:backingStore"];

        services
            .AddIdentityWithMongoStoresUsingCustomTypes<MyApplicationUser, IdentityRole>(
                identityBackingStoreConnectionString)
            .AddDefaultTokenProviders();

        services.AddSingleton<IClientStore, MyIdentityStore>();

        // IdentityServer4
        services.AddIdentityServer().AddTemporarySigningCredential()
            .AddInMemoryApiResources(MyResourceStore.GetAllResources())
            .AddAspNetIdentity<MyApplicationUser>()
            .AddTemporarySigningCredential();
        app.UseIdentity();

        app.UseIdentityServer();
        app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions()
        {
            Authority = "http://localhost:5000/",
            RequireHttpsMetadata = false,
            ApiName = "myapi",
            AutomaticAuthenticate = true,
            JwtBearerEvents = new JwtBearerEvents()
            {
                OnAuthenticationFailed = async context => context.Response.StatusCode = 401
            }
        });

        app.UseMvc();
如您所见,我已尝试覆盖OnAuthenticationFailed事件,但无效


任何关于如何让系统返回401的建议都将不胜感激。

Identity server在内部使用cookie身份验证,将401转换为302

我认为你不能因为它而让
app.UseIdentityServer()
app.UseIdentityServer身份验证()
生活在一起

但是,您可以轻松找到解决方法

最好是将identity server托管在单独的应用程序中(例如本地主机上的identity server:5000和本地主机上的应用程序:5001)。它更符合开放id连接的概念,您可以在

或者,您可以尝试使用
app.UseWhen
将Identity server和API放置在不同的子路径上,如localhost:5000/idsrv和localhost:5000/API。比如说

app.UseWhen(
    c => c.Request.Path.StartsWithSegments(new PathString("/api")), 
    branch => {
       branch.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions()
       {
           Authority = "http://localhost:5000/idsrv",
           RequireHttpsMetadata = false,
           ApiName = "myapi",
           AutomaticAuthenticate = true,
       } );
       branch.UseMvc();
   });

app.UseWhen(
    c => c.Request.Path.StartsWithSegments(new PathString("/idsrv")), 
    branch => {
       branch.UseIdentityServer();
       branch.UseMvc();
    } );

再次,这种方法更容易出错,我宁愿考虑单独的应用程序。

我不能让UseWhen方法工作,但是将这两个组件分离成独立的系统是完美的。非常感谢你的帮助!