我有一个使用ASP.NET Identity 2 / OWIN的ASP.NET MVC 5应用程序,它使用以下命令拥有自己的登录名:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString(AppConfiguration.LoginPath),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, int>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
getUserIdCallback: (id) => (Int32.Parse(id.GetUserId())))
}
});
除了上面在启动中设置的我们自己的身份验证之外,我们还要使用使用Identity Server 4的外部应用程序(基本上就是为了使该应用程序中的用户可以进入我们的应用程序)引入身份验证。 id connect,在上面的代码之后进行设置,例如:
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = AppConfiguration.ExternalServerAuthority,
ClientId = "xxxxxxxxxx",
ClientSecret = "secret",
RedirectUri = "http://localhost:1045/signin-oidc",
ResponseType = "id_token",
RequireHttpsMetadata = false,
PostLogoutRedirectUri = "http://localhost:1045/signout-callback-oidc",
SignInAsAuthenticationType = "Cookies",
Notifications = new OpenIdConnectAuthenticationNotifications
{
SecurityTokenValidated = async context =>
{
var appAuthManager = DependencyResolver.Current.GetService<IApplicationAuthenticationManager>();
var userManager = DependencyResolver.Current.GetService<IApplicationUserManager>();
var email = context.AuthenticationTicket.Identity.FindFirst("preferred_username");
var user = userManager.FindByName(email.Value, null);
if (user == null)
{
return;
}
await appAuthManager.SignInAsync(user, false, false);
}
}
});
从外部应用程序获取令牌时,我们需要能够执行SignInAsync,因为我们需要将该用户作为自己应用程序中的实际用户登录。问题的一部分还在于,如果我们进行了设置,那么当用户未登录时,如果试图访问他们必须登录的资源/页面,则总是将他们发送到另一个外部应用程序进行登录-它不再将其发送到我们现有的登录页面(这将使他们可以选择像往常一样在此处登录,或者如果他们也具有该应用程序的用户帐户,则单击链接将其带到另一个应用程序进行登录) 。我们不希望这样做,因为并非所有用户都将使用/拥有对另一个应用程序的访问权限,这实际上仅适用于某些用户,主要是可以方便地从另一个应用程序导航到我们的应用程序而不必分别登录我们的应用程序。因此,我们无法将所有未经授权的请求发送到该其他应用程序以进行登录。我们如何实现这一目标?有没有更好的方法在这里设置?
编辑以获取更多信息:
为了更清楚地说明此处的内容以及需要发生的情况。实际上有三个应用程序在起作用。我们有一个应用程序,一个具有自己的登录页面的MVC 5应用程序,使用owin / asp.net身份进行用户身份验证并存储/管理其用户。现在有另一个应用程序(适用于希望与我们合作的另一家公司),这是一个SPA应用程序,可通过同一公司运行的单独IdentityServer4服务器进行身份验证。此SPA应用程序希望在其中添加一个链接,该链接将用户发送到我们的MVC 5应用程序,而无需用户实际登录到我们的MVC 5应用程序-因此,他们希望将SSO插入我们的MVC 5应用程序(通过使用设置我们的应用程序以使用oidc connect来根据SPA的身份服务器对用户进行身份验证)。因此,当他们进入我们的应用程序时,我们实际上需要以我们的用户身份登录....但是我们还必须确保当我们当前未登录时,我们所有的用户都不会发送到该外部应用程序进行登录。访问我们的应用程序,因为并非所有用户都可以访问此外部应用程序。我希望这一点可以解决。