我有一个启用了 OWIN 身份验证的 ASP.NET MVC 应用程序(框架 4.7.2)。该应用程序是一个单一的 ERP,并且运行正常。该应用程序有一个资源可以向用户提供 html 报告。一个旧版 Java 应用程序现在需要访问此资源。我们开发了一个小部件(javascript + iframe)来将此资源嵌入到此遗留应用程序中。小部件使用 token 来访问 MVC 应用程序。这两个应用程序位于不同的域中。
当两个应用程序位于同一域但不在不同域时,该小部件可以正常工作。在 AuthenticationManager.SignIn() 之后,身份验证不起作用,用户被重定向到登录页面。如果我尝试通过“登录页面”登录,这也不起作用,因为位于不同的域和 iframe 内。
我尝试实现这个,但没有解决:https://learn.microsoft.com/pt-br/aspnet/samesite/system-web-samesite .
这是我用来通过 token 进行身份验证的代码:
[AllowAnonymous]
public async Task<ActionResult> LoginByToken(string token, string returnUrl)
{
var user = GetUserByToken(token);
if (user != null)
{
await SignInAsync(user);
return RedirectToLocal(returnUrl);
}
else
throw new Exception("The user was not authenticated!")
}
我怎样才能让它发挥作用?感谢您的帮助!
注意:下面报告的问题出现在 Chrome 和 Edge 中,但不会出现在 Firefox 中。
最佳答案
问题已解决!!!
我在这里找到了答案:https://learn.microsoft.com/pt-br/aspnet/samesite/csmvc
web.config 中的配置在我的情况下不起作用。我需要在方法ConfigureAuth中更改部分启动类 (/App_Start/Startup.Auth.cs)中的CookieAuthentication,如下所示:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
//---------------------------------
// It resolve the problem
CookieSameSite = SameSiteMode.None,
CookieHttpOnly = true,
CookieSecure = CookieSecureOption.Always,
//---------------------------------
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
注意:iframe 中的“src”需要在 https 中发布。
关于asp.net-mvc - 通过外部小部件 (iframe) 中的 token 在 ASP.NET MVC 应用程序中进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66457148/