我打算通过我的 web 项目通过 IdentityServer4 获得一个不记名 token ,但我收到了这个异常。
异常(exception)是在 IdentityServer4 开源类库中。字段 url 为空,这会导致 AddQueryString 方法中的 NullReference 异常,请参阅 https://github.com/IdentityServer/IdentityServer4/blob/master/src/Extensions/StringsExtensions.cs
日志文件显示;
所以在 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())}
}
};
}
这是为什么,我该如何解决?
最佳答案
将 UserInteractionURL 添加到 AddIdentityServer。它会解决你的问题
services.AddIdentityServer(options =>
{
options.UserInteraction = new UserInteractionOptions()
{
LogoutUrl = "/Identity/account/logout",
LoginUrl = "/Identity/account/login",
LoginReturnUrlParameter = "returnUrl"
};
})
关于c# - 调用 IdentityServer4 生成 System.NullReferenceException : Object reference not set to an instance of an object,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53871066/