C# .net核心仅通过WS-Fed进行身份验证

C# .net核心仅通过WS-Fed进行身份验证,c#,authentication,.net-core,ws-federation,identityserver2,C#,Authentication,.net Core,Ws Federation,Identityserver2,我们正在将一个新的.net核心web应用程序添加到现有环境中,该环境的中心是用于身份验证的Identity Server 2。创建新应用程序时,我很难让它使用现有的身份验证 目标是新应用程序中的每个页面都需要登录用户。新应用程序有一些用户表,但不需要默认的身份数据库(AspNetUsers等),也不需要自己的登录对话框(或密码恢复等) 为此,我在ConfigureServices()中的Startup.cs文件中添加了以下内容: 这会产生类似但不同的错误,“未指定authenticationSc

我们正在将一个新的.net核心web应用程序添加到现有环境中,该环境的中心是用于身份验证的Identity Server 2。创建新应用程序时,我很难让它使用现有的身份验证

目标是新应用程序中的每个页面都需要登录用户。新应用程序有一些用户表,但不需要默认的身份数据库(AspNetUsers等),也不需要自己的登录对话框(或密码恢复等)

为此,我在ConfigureServices()中的Startup.cs文件中添加了以下内容:

这会产生类似但不同的错误,“未指定authenticationScheme,也未找到DefaultAuthenticationScheme。”

如果我改为取消注释其他任何一行(或两行):

这会产生错误,“远程身份验证处理程序的SignInScheme无法设置为自身。如果未显式设置,则使用AuthenticationOptions.DefaultSignInScheme或DefaultScheme。”

如果我将应用程序配置为“正常”身份验证,如下所示:

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(
        Configuration.GetConnectionString("DefaultConnection")));

services.AddDefaultIdentity<ApplicationUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();
我似乎找不到删除“默认”登录并自动重定向到Identity Server登录的秘方

谢谢你的阅读

更新:

@Rytmis建议,我缺少的是定义cookie,这表明应用程序“登录”,这是一个很好的观察结果,但我仍然缺少一部分,因为我不明白如何从WS-Fed到cookie身份验证(以识别当前用户)。我更改了我的ConfigureServices代码,如下所示,这确实会导致程序立即重定向到identity Server,这是完美的,登录后它会重定向回ws-fed端点,然后重定向回初始页面。所有这些都是好消息,但是初始页面仍然没有看到用户登录。cookie已经创建,但我不知道如何、在何处或者登录名是否从ws-fed转换为cookie?我定义了自定义的IUserStore和IRoleStore类,但它们没有被使用,甚至没有被实例化

services.AddAuthentication(sharedOptions =>
                           {
                               sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                               sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                               sharedOptions.DefaultChallengeScheme = WsFederationDefaults.AuthenticationScheme;
                           })
        .AddWsFederation(authenticationScheme: "WsFederation",
                         displayName: "Single Signin",
                         configureOptions: options =>
                                           {
                                               // MetadataAddress represents the Identity Server instance used to authenticate users.
                                               options.MetadataAddress = authenticationConfigSection.GetValue<string>("MetadataAddress");

                                               // Wtrealm is the app's relying party identifier in the IS instance.
                                               options.Wtrealm = authenticationConfigSection.GetValue<string>("Wtrealm");

                                               //options.SignInScheme = "WsFederation";
                                           })
        .AddCookie(options =>
                   {
                       options.Cookie.SameSite = SameSiteMode.None;
                       options.Cookie.Name = "AuthCookie";
                       options.AccessDeniedPath = "/error/accessdenied";
                   });

services.AddScoped<UserManager<ApplicationUser>, UserManager<ApplicationUser>>();
services.AddTransient<IUserStore<ApplicationUser>, MyUserStore<ApplicationUser>>();
services.AddTransient<IRoleStore<ApplicationRole<string>>, MyRoleStore<ApplicationRole<string>>>();
services.AddAuthentication(sharedOptions=>
{
sharedOptions.DefaultScheme=CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultSignenscheme=CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme=WsFederationDefaults.AuthenticationScheme;
})
.AddWsFederation(身份验证方案:“WsFederation”,
displayName:“单一登录”,
配置选项:选项=>
{
//MetadataAddress表示用于对用户进行身份验证的Identity Server实例。
options.MetadataAddress=authenticationConfigSection.GetValue(“MetadataAddress”);
//Wtrealm是应用程序在is实例中的依赖方标识符。
options.Wtrealm=authenticationConfigSection.GetValue(“Wtrealm”);
//options.signnscheme=“WsFederation”;
})
.AddCookie(选项=>
{
options.Cookie.SameSite=SameSiteMode.None;
options.Cookie.Name=“AuthCookie”;
options.AccessDeniedPath=“/error/accessdenied”;
});
services.addScope();
services.AddTransient();
services.AddTransient();

远程身份验证的问题是,除非您希望对每个请求执行远程身份验证重定向,否则您必须在本地存储某些内容,而没有人会这样做


除了远程方案之外,您还需要指定一个本地身份验证方案,该方案将用于存储远程身份验证的结果。这通常是通过添加Cookie身份验证处理程序并将其用作签名方案来实现的。

远程身份验证的问题是,除非您希望对每个请求执行远程身份验证重定向,否则您必须在本地存储某些内容,而没有人会这样做


除了远程方案之外,您还需要指定一个本地身份验证方案,该方案将用于存储远程身份验证的结果。这通常是通过添加Cookie身份验证处理程序并将其用作签名方案来完成的。

我认为没有人需要行号;-)@Isma我只是想清楚地说明我的尝试。我理解,但我无法复制粘贴代码来测试它是否有行号。如果您的示例代码是最新的,我怀疑您需要取消注释为WS-FED身份验证处理程序设置options.signnscheme的行,并将signnscheme设置为CookieAuthenticationDefaults.AuthenticationScheme我认为没有人需要行号;-)@Isma我只是想清楚地说明我的尝试。我理解,但我无法复制粘贴代码来测试它是否有行号。如果您的示例代码是最新的,我怀疑您需要取消注释为WS-FED身份验证处理程序设置options.signnscheme的行,并将signnscheme设置为CookieAuthenticationDefaults.AuthenticationScheme我没有这样想过。我试试看。非常感谢。谢谢你的建议,我尝试了你的建议,我认为这是朝着正确的方向迈出的一步,但我陷入了进一步的困惑(如果你愿意看一看,让我知道我仍然缺少什么,我用我尝试过的方法更新了原来的问题)。我没有那样想过。我试试看。非常感谢。谢谢你的建议,
06:services.AddAuthentication("WsFederation")    
17:options.SignInScheme = "WsFederation";
services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(
        Configuration.GetConnectionString("DefaultConnection")));

services.AddDefaultIdentity<ApplicationUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

services.AddIdentity<ApplicationUser, IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders();
public class ApplicationUser : IdentityUser
{
    public User User { get; set; }

    public ApplicationUser(User user)
    {
        base.Id = User.IdentityGuid.ToString("D");
        base.Email = user.Person.EmailAddress;
        base.NormalizedEmail = user.Person.EmailAddress.ToLowerInvariant();
        base.UserName = user.Username;
        base.NormalizedUserName = user.Username.ToLowerInvariant();
        base.EmailConfirmed = true;
        User = user;
    }
}
services.AddAuthentication(sharedOptions =>
                           {
                               sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                               sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                               sharedOptions.DefaultChallengeScheme = WsFederationDefaults.AuthenticationScheme;
                           })
        .AddWsFederation(authenticationScheme: "WsFederation",
                         displayName: "Single Signin",
                         configureOptions: options =>
                                           {
                                               // MetadataAddress represents the Identity Server instance used to authenticate users.
                                               options.MetadataAddress = authenticationConfigSection.GetValue<string>("MetadataAddress");

                                               // Wtrealm is the app's relying party identifier in the IS instance.
                                               options.Wtrealm = authenticationConfigSection.GetValue<string>("Wtrealm");

                                               //options.SignInScheme = "WsFederation";
                                           })
        .AddCookie(options =>
                   {
                       options.Cookie.SameSite = SameSiteMode.None;
                       options.Cookie.Name = "AuthCookie";
                       options.AccessDeniedPath = "/error/accessdenied";
                   });

services.AddScoped<UserManager<ApplicationUser>, UserManager<ApplicationUser>>();
services.AddTransient<IUserStore<ApplicationUser>, MyUserStore<ApplicationUser>>();
services.AddTransient<IRoleStore<ApplicationRole<string>>, MyRoleStore<ApplicationRole<string>>>();