注意:在解决重定向问题后,我遇到了另一个问题,即收到错误"无法将Newtonsoft.Json.Linq.JArray转换为Newtonsoft.Json.Linq.JToken"。所以在我的回答中,我为两者提供了正确的解决方案。
我有身份服务器项目和客户端项目,一切都可以进行身份验证而不会出现任何问题,甚至可以重定向到正确的客户端URL,但是网址为:" https://localhost:44309/signin-oidc"给出空白页。
注意:为Identity Server和客户端应用程序启用了SSl。
正如预期的那样验证用户,如下面的屏幕截图所示。 我的标识服务器包含客户端的以下配置值。
// 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");
});
}
任何人都可以帮我解决这个问题。
答案 0 :(得分:2)
我可以在Identity Server 4人员的帮助下解决这个问题。 如果有人遇到这个问题就是解决方案。
我错过了添加" UseAuthentication"在配置客户端MVC管道。所以在添加后我按预期重定向,然后我有另一个问题,如下所示。
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()
我将使用AzureAD作为外部身份验证提供程序将我的应用程序连接到IdentityServer4时遇到此异常。我的应用程序使用Hybrid流程连接到IdentityServer4。我被正确地重定向到Azure,登录,并且代码和id_tokens被正确发布。调用userInfo端点时,在我的应用程序中引发此异常。
为了解决这个问题,我必须删除名称两次的声明。
我确认AAD发出了两个名称声明。删除其中一个可以解决问题。
var namesClaim = externalUser.FindFirst(ClaimTypes.Name) ??
throw new Exception("Unknown names");
if (namesClaim!=null)
{
claims.Remove(namesClaim);
}
希望这可以帮助某人。
答案 1 :(得分:1)
我在担任多个职务时遇到了同样的问题。这是解决方案:
.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");
});