调用IdentityServer4会生成System.NullReferenceException:对象引用未设置为对象的实例

时间:2018-12-20 14:55:44

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

我打算通过我的Web项目通过IdentityServer4获得一个承载令牌,并且遇到了这个异常。 IdentityServer4开源类库中有一个例外。字段url为null,这会在AddQueryString方法中导致NullReference异常,请参见https://github.com/IdentityServer/IdentityServer4/blob/master/src/Extensions/StringsExtensions.cs

显示日志文件;

  

IdentityServer4.Endpoints.AuthorizeEndpoint:信息:ValidatedAuthorizeRequest   {     “ ClientId”:“ SIR”,     “ ClientName”:“ SIR”,     “ 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个人资料”,     “国”: “OpenIdConnect.AuthenticationProperties = WBfqf-a6W0K-0x6giakJP1GCkjUyG0wzOgAr9AuitPNyUb6wsIlzJN-Yvv-ARRdTd5huJIIl3N0mpI95EbLzGKIVmAhXr4JiIWKo2dOCTFI7PH218T9V1vVkKP3kFmQgtRRYRagG9YEA2PvyMtxzQXMf4v3pPequ8Am7H_8TIfgMqspxAnTsXQ4K-cD_TBTVFc45AiDiylpWup1_Ovrpqu700JCGimHZJRuXP25MHMs”     “ Nonce”:“ 636809130138863279.M2IyNTYyZTgtZTk0Ni00OWU5LWI4MmMtNGU2MWY4M2FkMzQzNzExYjRjYjYtOWY4MC00NjQwLWEyZGYtYzGzYjljZTY4ZDFj”     “生的”: {       “ client_id”:“ SIR”,       “ redirect_uri”:“ http://localhost:53200/signin-oidc”,       “ response_mode”:“ form_post”,       “ response_type”:“ id_token代码”,       “ scope”:“ 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:Critical:未处理的异常:对象引用未设置为对象的实例。

     

System.NullReferenceException:对象引用未设置为实例   一个对象。在   IdentityServer4.Extensions.StringExtensions.AddQueryString(String url,   字符串查询)   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为null。 在我的Web客户端中,我的启动方法是;

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())}
        }
    };
}

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

1 个答案:

答案 0 :(得分:0)

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

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