如何使用 IdentityServer 对 AspNetIdentity 项目进行身份验证?
How to use IdentityServer to authenticate and AspNetIdentity project?
我有一些客户端应用程序和一个身份服务器应用程序。每个应用程序都需要有一个单点登录流程。到目前为止,我已经能够毫无问题地注册我的其他应用程序。但是,我的身份应用程序遇到了 运行 的无限循环问题。我的想法是,我的身份应用程序的 AddIdentity()
部分以某种方式干扰了 AddAuthentication()
方法调用中的 IdentityServer4 客户端注册。
问题应用程序的唯一职责是管理属于所有应用程序的用户,而不是实际登录(登录是使用 IdentityServer4 完成的)。这是配置。
// Config.cs - problem client configuration
new Client
{
ClientId = "identity",
ClientName = "Identity Management",
AllowedGrantTypes = GrantTypes.Implicit,
RedirectUris = { "http://localhost:59990/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:59990/signout-callback-oidc" },
AllowedScopes = GetAllowedScopes(),
},
我真的不认为有什么问题 config.cs 因为我之前的所有应用程序都可以使用相同的客户端注册码登录。
// ConfigureServices.cs - for client that has redirect loop
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<SafetyContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SafetyPlusConnection")));
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddIdentity<SafetyUser, SafetyRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddRoleManager<RoleManager<SafetyRole>>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.ClientId = "identity";
options.SaveTokens = true;
});
services.AddScoped<RoleService>();
剩下的配置只是一些样板代码。
// Configure.cs - in the problem application
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
// Register.cs - code for registering new users (needs authorization first)
[Authorize]
public class RegisterModel : PageModel
当我导航到用 [Authorize]
属性装饰的注册页面时,我正确登录到 IdentityServer 并要求访问我的 openid 和配置文件权限。当我接受请求时,应用程序开始无限循环。
以下三个请求在无限循环中重复。
http://localhost:59990/signin-oidc
http://localhost:59990/Identity/Account/Register
http://localhost:5000/connect/authorize?client_id=identity&redirect_uri=http%3A%2F%2Flocalhost%3A59990%2Fsignin-oidc&response_type =id_token&scope=openid%20profile&response_mode=form_post&nonce=...(省略编码文本)
当您调用 AddIdentity<>
时,您已经 setup authentication 有多个选项。所以尝试覆盖所有三个默认值:
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
中所述
我有一些客户端应用程序和一个身份服务器应用程序。每个应用程序都需要有一个单点登录流程。到目前为止,我已经能够毫无问题地注册我的其他应用程序。但是,我的身份应用程序遇到了 运行 的无限循环问题。我的想法是,我的身份应用程序的 AddIdentity()
部分以某种方式干扰了 AddAuthentication()
方法调用中的 IdentityServer4 客户端注册。
问题应用程序的唯一职责是管理属于所有应用程序的用户,而不是实际登录(登录是使用 IdentityServer4 完成的)。这是配置。
// Config.cs - problem client configuration
new Client
{
ClientId = "identity",
ClientName = "Identity Management",
AllowedGrantTypes = GrantTypes.Implicit,
RedirectUris = { "http://localhost:59990/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:59990/signout-callback-oidc" },
AllowedScopes = GetAllowedScopes(),
},
我真的不认为有什么问题 config.cs 因为我之前的所有应用程序都可以使用相同的客户端注册码登录。
// ConfigureServices.cs - for client that has redirect loop
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<SafetyContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SafetyPlusConnection")));
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddIdentity<SafetyUser, SafetyRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddRoleManager<RoleManager<SafetyRole>>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.ClientId = "identity";
options.SaveTokens = true;
});
services.AddScoped<RoleService>();
剩下的配置只是一些样板代码。
// Configure.cs - in the problem application
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
// Register.cs - code for registering new users (needs authorization first)
[Authorize]
public class RegisterModel : PageModel
当我导航到用 [Authorize]
属性装饰的注册页面时,我正确登录到 IdentityServer 并要求访问我的 openid 和配置文件权限。当我接受请求时,应用程序开始无限循环。
以下三个请求在无限循环中重复。
http://localhost:59990/signin-oidc
http://localhost:59990/Identity/Account/Register
http://localhost:5000/connect/authorize?client_id=identity&redirect_uri=http%3A%2F%2Flocalhost%3A59990%2Fsignin-oidc&response_type =id_token&scope=openid%20profile&response_mode=form_post&nonce=...(省略编码文本)
当您调用 AddIdentity<>
时,您已经 setup authentication 有多个选项。所以尝试覆盖所有三个默认值:
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
中所述