Asp.net core mvc 从Identity server 4重定向url的行为不符合预期,并且;无法将Newtonsoft.Json.Linq.JArray强制转换为Newtonsoft.Json.Linq.JToken“;错误
注意:解决重定向问题后,我遇到了另一个问题,该问题出现错误“无法将Newtonsoft.Json.Linq.JArray强制转换为Newtonsoft.Json.Linq.JToken”。因此,在我的回答中,我为两者提供了正确的解决方案Asp.net core mvc 从Identity server 4重定向url的行为不符合预期,并且;无法将Newtonsoft.Json.Linq.JArray强制转换为Newtonsoft.Json.Linq.JToken“;错误,asp.net-core-mvc,identityserver4,openid-connect,Asp.net Core Mvc,Identityserver4,Openid Connect,注意:解决重定向问题后,我遇到了另一个问题,该问题出现错误“无法将Newtonsoft.Json.Linq.JArray强制转换为Newtonsoft.Json.Linq.JToken”。因此,在我的回答中,我为两者提供了正确的解决方案 我有身份服务器项目和客户端项目,所有的工作都是在没有任何问题的情况下进行认证的,甚至重定向到正确的客户机URL,但是URL EX:“”给出了空白页。 注意:为Identity Server和客户端应用程序启用SSl 正如您在下面的屏幕截图中看到的那样,它正在验证
我有身份服务器项目和客户端项目,所有的工作都是在没有任何问题的情况下进行认证的,甚至重定向到正确的客户机URL,但是URL EX:“”给出了空白页。 注意:为Identity Server和客户端应用程序启用SSl
正如您在下面的屏幕截图中看到的那样,它正在验证用户。 My Identity server包含客户端的以下配置值 // OpenID Connect hybrid flow and client credentials client (MVC)
new Client
{
ClientId = "mvc",
ClientName = "MVC Client",
AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,
ClientSecrets =
{
new Secret("secret".Sha256())
},
RedirectUris = { /*"http://localhost:5002/signin-oidc",*/"https://localhost:44309/signin-oidc" },
PostLogoutRedirectUris = { /*"http://localhost:5002/signout-callback-oidc",*/"https://localhost:44309/signout-callback-oidc" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
//"api1"
},
AllowOfflineAccess = true
}
startup.cs如下所示
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// configure identity server with in-memory stores, keys, clients and scopes
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.AddTestUsers(Config.GetUsers());
services.AddAuthentication()
//.AddGoogle("Google", options =>
//{
// options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
// options.ClientId = "434483408261-55tc8n0cs4ff1fe21ea8df2o443v2iuc.apps.googleusercontent.com";
// options.ClientSecret = "3gcoTrEDPPJ0ukn_aYYT6PWo";
//})
.AddOpenIdConnect("oidc", "dataVail Login", options =>
{
options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
options.SignOutScheme = IdentityServerConstants.SignoutScheme;
options.Authority = "https://login.microsoftonline.com/d0e2ebcc-0961-45b2-afae-b9ed6728ead7";//"https://demo.identityserver.io/";
options.ClientId = "f08cc131-72da-4831-b19d-e008024645e4";
options.UseTokenLifetime = true;
options.CallbackPath = "/signin-oidc";
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
RoleClaimType = "role"
};
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.Use(async (context, next) =>
{
context.Request.Scheme = "https";
await next.Invoke();
});
app.UseIdentityServer();
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
}
这是我的客户端应用程序的startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = "Cookies";
options.Authority = "https://localhost:44392/";
options.RequireHttpsMetadata = false;
options.ClientId = "mvc";
options.ClientSecret = "secret";
options.ResponseType = "code id_token";
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
//options.Scope.Add("api1");
options.Scope.Add("offline_access");
});
}
谁能帮我解决这个问题。我可以在Identity Server 4的帮助下解决这个问题。 如果有人遇到这个问题,这里是解决办法 我没有在配置客户机MVC管道中添加“UseAuthentication”。所以在添加之后,我按照预期被重定向,然后我遇到了另一个问题,如下所示
System.InvalidCastException: Cannot cast Newtonsoft.Json.Linq.JArray to Newtonsoft.Json.Linq.JToken. at Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler1.d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.<Invoke>d__6.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext()
希望这可能会对某人有所帮助。我在担任多个角色时也遇到同样的问题。以下是解决方案:
.AddOpenIdConnect("oidc", options =>
{
// ...
options.Scope.Add("roles");
// ... using MapJsonKey instead of MapUniqueJsonKey for having 2 or more roles
options.ClaimActions.MapJsonKey(claimType: "role", jsonKey: "role");
});
能否请您更具体地说明删除额外索赔的代码将插入何处?@Ilya如果您下载了IdentityServer 4示例,那么您应该在Quickstart.Account.AccountController.FindUserFromeExternalProvider()方法中找到此代码片段的放置位置。
.AddOpenIdConnect("oidc", options =>
{
// ...
options.Scope.Add("roles");
// ... using MapJsonKey instead of MapUniqueJsonKey for having 2 or more roles
options.ClaimActions.MapJsonKey(claimType: "role", jsonKey: "role");
});