【问题标题】:Call to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an object调用 IdentityServer4 生成 System.NullReferenceException:对象引用未设置为对象的实例
【发布时间】:2019-05-21 02:47:24
【问题描述】:

我打算通过我的 Web 项目通过 IdentityServer4 获取不记名令牌,但我遇到了这个异常。 IdentityServer4 开源类库中的例外。字段url为null,导致AddQueryString方法出现NullReference异常,见https://github.com/IdentityServer/IdentityServer4/blob/master/src/Extensions/StringsExtensions.cs

日志文件显示;

IdentityServer4.Endpoints.AuthorizeEndpoint:信息:ValidatedAuthorizeRequest { "ClientId": "先生", “客户名称”:“先生”, "RedirectUri": "http://localhost:53200/signin-oidc", “AllowedRedirectUris”:[ "https://localhost:44314", “http://localhost:53200/signin-oidc” ], "SubjectId": "匿名", "ResponseType": "代码 id_token", "ResponseMode": "form_post", “GrantType”:“混合”, "RequestedScopes": "openid profile", “国”: “OpenIdConnect.AuthenticationProperties = WBfqf-a6W0K-0x6giakJP1GCkjUyG0wzOgAr9AuitPNyUb6wsIlzJN-Yvv-ARRdTd5huJIIl3N0mpI95EbLzGKIVmAhXr4JiIWKo2dOCTFI7PH218T9V1vVkKP3kFmQgtRRYRagG9YEA2PvyMtxzQXMf4v3pPequ8Am7H_8TIfgMqspxAnTsXQ4K-cD_TBTVFc45AiDiylpWup1_Ovrpqu700JCGimHZJRuXP25MHMs” “Nonce”:“636809130138863279.M2IyNTYyZTgtZTk0Ni00OWU5LWI4MmMtNGU2MWY4M2FkMzQzNzExYjRjYjYtOWY4MC00NjQwLWEyZGYtYzgzYjljZTY4ZDFj”, “生的”: { “client_id”:“先生”, "redirect_uri": "http://localhost:53200/signin-oidc", "response_mode": "form_post", "response_type": "id_token 代码", "范围": "openid 配置文件", “状态”: “OpenIdConnect.AuthenticationProperties = WBfqf-a6W0K-0x6giakJP1GCkjUyG0wzOgAr9AuitPNyUb6wsIlzJN-Yvv-ARRdTd5huJIIl3N0mpI95EbLzGKIVmAhXr4JiIWKo2dOCTFI7PH218T9V1vVkKP3kFmQgtRRYRagG9YEA2PvyMtxzQXMf4v3pPequ8Am7H_8TIfgMqspxAnTsXQ4K-cD_TBTVFc45AiDiylpWup1_Ovrpqu700JCGimHZJRuXP25MHMs”, “nonce”:“636809130138863279.M2IyNTYyZTgtZTk0Ni00OWU5LWI4MmMtNGU2MWY4M2FkMzQzNzExYjRjYjYtOWY4MC00NjQwLWEyZGYtYzgzYjljZTY4ZDFj”, "x-client-SKU": "ID_NET461", “x-client-ver”:“5.3.0.0” } } IdentityServer4.ResponseHandling.AuthorizeInteractionResponseGenerator:信息:显示登录:用户未通过身份验证 IdentityServer4.ResponseHandling.AuthorizeInteractionResponseGenerator:信息:显示登录:用户未通过身份验证 IdentityServer4.ResponseHandling.AuthorizeInteractionResponseGenerator:信息:显示登录:用户未通过身份验证 IdentityServer4.ResponseHandling.AuthorizeInteractionResponseGenerator:信息:显示登录:用户未通过身份验证 抛出异常:IdentityServer4.dll 中的“System.NullReferenceException” 抛出异常:IdentityServer4.dll 中的“System.NullReferenceException” “dotnet.exe”(CoreCLR:clrhost):已加载“C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.1.6\System.Diagnostics.StackTrace.dll”。已加载符号。 “dotnet.exe”(CoreCLR:clrhost):已加载“C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.1.6\System.Reflection.Metadata.dll”。已加载符号。 “dotnet.exe”(CoreCLR:clrhost):已加载“C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.1.6\System.IO.MemoryMappedFiles.dll”。已加载符号。 IdentityServer4.Hosting.IdentityServerMiddleware:严重:未处理的异常:对象引用未设置为对象的实例。

System.NullReferenceException:对象引用未设置为实例 的一个对象。在 IdentityServer4.Extensions.StringExtensions.AddQueryString(字符串网址, 字符串查询)在 C:\local\identity\server4\IdentityServer4\src\Extensions\StringsExtensions.cs:line 197 在 IdentityServer4.Endpoints.Results.LoginPageResult.ExecuteAsync(HttpContext 上下文)在 C:\local\identity\server4\IdentityServer4\src\Endpoints\Results\LoginPageResult.cs:line 61 在 IdentityServer4.Hosting.IdentityServerMiddleware.Invoke(HttpContext 上下文、IEndpointRouter 路由器、IUserSession 会话、IEventService 事件)在 C:\local\identity\server4\IdentityServer4\src\Hosting\IdentityServerMiddleware.cs:line 59 IdentityServer4.Hosting.IdentityServerMiddleware:严重: 未处理的异常:对象引用未设置为 对象。

所以在 AddQueryString 方法中,url 为空。 在我的网络客户端中,我的启动方法是;

public void Configuration(IAppBuilder app)
{
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = Settings.SignInAsAuthenticationType    // "Cookies";
    });

    app.UseOpenIdConnectAuthentication(openIdConnectOptions: new OpenIdConnectAuthenticationOptions
    {
        AuthenticationType = "oidc",
        Authority = Settings.AuthorityUrl,      //ID Server,  "https://localhost:44314/"; https://localhost:44307/
        ClientId = Settings.ClientId,           // "SIR"
        Scope = Settings.Scope,                 // "openid profile";
        ResponseType = Settings.ResponseType,   // "id_token code";
        SignInAsAuthenticationType = Settings.SignInAsAuthenticationType,
        //--------------------------------------// "Cookies";
        RedirectUri = Settings.RedirectUri,     // URL of website, http://localhost:53200/signin-oidc;
        RequireHttpsMetadata = Settings.RequireHttpsMetadata,
        //--------------------------------------// true
        ClientSecret = "secret"
    });

    app.Use(async (ctx, next) =>
    {
        var message = ctx.Authentication.User.Identity.IsAuthenticated
            ? $"User: {ctx.Authentication.User.Identity.Name}"
            : "User Not Authenticated";
        await next();
    });
}

注意我正在使用 Microsoft.Owin

我的 IdentityServer4 中的客户端是;

public static IEnumerable<Client> Clients()
{
    return new[]
    {
        new Client
        {
            ClientId = "SIR",
            ClientName = "SIR",
            AllowedGrantTypes = GrantTypes.Hybrid,
            AllowedScopes = new[]
            {
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Profile
            },
            RedirectUris = {
                "https://localhost:44314",
                "http://localhost:53200/signin-oidc"
            },
            ClientSecrets = { new Secret("secret".Sha256())}
        }
    };
}

为什么会这样,我该如何解决?

【问题讨论】:

  • 你能分享导致这种情况的代码吗?它是哪一行代码?显然有些东西是null,必须赋予价值。
  • @Symon - 按要求放入文档
  • 错误出现在 IdentityServer4 代码中,它是一个开源库。如果您熟悉该库,则该错误是由 AddQueryString 方法中的 url 字段为空引起的。我想知道为什么这个字段为空。
  • 这不是关于 NullReferenceException 的重复问题。这是一个关于如何使用 IdentityServer4 开源库的问题,这就是我在问题中标记它的原因。

标签: c# oauth-2.0 identityserver4 openid-connect


【解决方案1】:

将 UserInteractionURL 添加到 AddIdentityServer。它会解决你的问题

 services.AddIdentityServer(options =>
                {
                    options.UserInteraction = new UserInteractionOptions()
                    {
                        LogoutUrl = "/Identity/account/logout",
                        LoginUrl = "/Identity/account/login",
                        LoginReturnUrlParameter = "returnUrl"
                    };
                })

【讨论】:

    猜你喜欢
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    相关资源
    最近更新 更多