我们将一个新的.NET Core Web应用程序添加到现有环境中,其中心是用于身份验证的Identity Server 2。创建新应用程序时,我很难让它使用现有身份验证。
目标是新应用中的每个页面都需要登录用户。新应用程序具有一些用户表,但不需要默认身份数据库(Aspnetusers等),也不需要其自己的登录对话框(也不需要密码恢复等)。
为此,我在Configureservices()中添加了以下内容。
01:services.AddTransient<IUserStore<ApplicationUser>, E360UserStore<ApplicationUser>>();
02:services.AddTransient<IRoleStore<ApplicationRole<string>>, E360RoleStore<ApplicationRole<string>>>();
03:
04:var authenticationConfigSection = Configuration.GetSection("Authentication");
05:
06:services.AddAuthentication(/*"WsFederation"*/)
07: .AddWsFederation(authenticationScheme: "WsFederation",
08: displayName: "Single Signin",
09: configureOptions: options =>
10: {
11: // MetadataAddress represents the Identity Server instance used to authenticate users.
12: options.MetadataAddress = authenticationConfigSection.GetValue<string>("MetadataAddress");
13:
14: // Wtrealm is the app's relying party identifier in the IS instance.
15: options.Wtrealm = authenticationConfigSection.GetValue<string>("Wtrealm");
16:
17: //options.SignInScheme = "WsFederation";
18: });
19:
20:services.AddMvc(config =>
21: {
22: var policy = new AuthorizationPolicyBuilder(/*"WsFederation"*/)
23: .RequireAuthenticatedUser()
24: .Build();
25:
26: config.Filters.Add(new AuthorizeFilter(policy));
28: })
29: .AddJsonOptions(o => o.SerializerSettings.ContractResolver = new DefaultContractResolver())
30: .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
这会产生错误:"未指定Authenticationscheme,并且找不到DefaultChallengescheme。"因此,我尝试一次删除三个" WSFEDERATION"字符串。
我尝试先删除最后一个:
22:var policy = new AuthorizationPolicyBuilder("WsFederation")
23: .RequireAuthenticatedUser()
24: .Build();
这会产生相似但不同的错误,"未指定Authenticationscheme,并且找不到默认义务。"
'如果我取消其他任何行(或两者):
06:services.AddAuthentication("WsFederation")
17:options.SignInScheme = "WsFederation";
这会产生错误,"无法将远程身份验证处理程序的签名设置为自身。如果未明确设置,则使用AuthenticationOptions.defaultSignInscheme或DefaultScheme。"
。"如果我为"正常"身份验证配置应用程序,则是这样的:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
然后,我获得一个登录对话框,并在右图上标记为"单个符号"的按钮,该按钮正常。WSFEDERATION登录地图的索赔向在生成的身份数据库中手动创建的用户。
我对新模型中的E360userstore和E360Rolestore的意图是映射到findbyidasync()中的IdentityUser(我无法对此进行测试):
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;
}
}
我似乎找不到删除"默认"登录的秘密调味料,而是自动将其重定向到身份服务器登录。
感谢您的阅读!
更新:
@rytmis建议我缺少的是定义cookie,表明该应用程序已"记录在"中,这是一个很好的观察,但是我仍然缺少一件作品ws for fo cookie auth(以识别当前用户)。我更改了Configureservices代码,如下所示,这确实导致程序立即重定向到Identity Server,这是完美的,然后登录后将其重定向到WS-FED端点,然后将其重定向到初始页面。所有这些都是个好消息,但是初始页面仍然没有将用户视为已登录。创建了cookie,但是我看不出登录方式如何,何处或是否从ws-fed转换为cookie?我定义了Custom 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>>>();
远程身份验证的东西是,除非您想对每个请求进行远程验证重定向,否则您将必须在本地存储一些东西。
除了远程方案外,您还需要指定一个本地身份验证方案,该方案将用于存储远程身份验证的结果。这通常是通过添加cookie Authentication处理程序并将其用作标志Cheme.