ara*_*333 3 c# oauth-2.0 openid-connect identityserver4
我打算通过我的 web 项目通过 IdentityServer4 获得一个不记名令牌,但我收到了这个异常。例外是在 IdentityServer4 开源类库中。字段 url 为空,这会导致 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": "anonymous", "ResponseType": "code id_token", "ResponseMode": "form_post", "GrantType": "hybrid", "RequestedScopes": "openid profile", "State": " OpenIdConnect.AuthenticationProperties = WBfqf-a6W0K-0x6giakJP1GCkjUyG0wzOgAr9AuitPNyUb6wsIlzJN-Yvv-ARRdTd5huJIIl3N0mpI95EbLzGKIVmAhXr4JiIWKo2dOCTFI7PH218T9V1vVkKP3kFmQgtRRYRagG9YEA2PvyMtxzQXMf4v3pPequ8Am7H_8TIfgMqspxAnTsXQ4K-cD_TBTVFc45AiDiylpWup1_Ovrpqu700JCGimHZJRuXP25MHMs “ ”随机数“: ”636809130138863279.M2IyNTYyZTgtZTk0Ni00OWU5LWI4MmMtNGU2MWY4M2FkMzQzNzExYjRjYjYtOWY4MC00NjQwLWEyZGYtYzgzYjljZTY4ZDFj“, ”RAW“:{ ”CLIENT_ID“: ”SIR“, ”REDIRECT_URI“:”http://localhost:53200/signin-oidc显示登录:用户未通过身份验证 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, String query) in C:\local\identity\server4\IdentityServer4\src\Extensions\StringsExtensions.cs:line 197 在 IdentityServer4.Endpoints.Results.LoginPageResult.ExecuteAsync(HttpContext context context) ) 在 C:\local\identity\server4\IdentityServer4\src\Endpoints\Results\LoginPageResult.cs:line 61 at IdentityServer4.Hosting.IdentityServerMiddleware.Invoke(HttpContext context, IEndpointRouter router, IUserSession session, IEventService events) in C:\ local\identity\server4\IdentityServer4\src\Hosting\IdentityServerMiddleware.cs:line 59 IdentityServer4.Hosting.IdentityServerMiddleware:Critical: 未处理的异常:未将对象引用设置为对象的实例。
所以在 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();
});
}
Run Code Online (Sandbox Code Playgroud)
请注意,我正在使用 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())}
}
};
}
Run Code Online (Sandbox Code Playgroud)
这是为什么,我该如何解决?
小智 5
将 UserInteractionURL 添加到 AddIdentityServer。它会解决你的问题
services.AddIdentityServer(options =>
{
options.UserInteraction = new UserInteractionOptions()
{
LogoutUrl = "/Identity/account/logout",
LoginUrl = "/Identity/account/login",
LoginReturnUrlParameter = "returnUrl"
};
})
Run Code Online (Sandbox Code Playgroud)