ASP.netCore3.1Web应用程序调用IFrame中的另一个ASP.netCore应用程序URL会重置身份验证,并且任何其他页面都会重定向到登录页。
创始人
2024-09-17 10:01:03
0次

可以使用Cookie来在两个应用程序之间进行身份验证和授权。可以将以下代码添加到Startup.cs文件中的ConfigureServices方法中:

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.Cookie.Domain = "localhost"; options.Cookie.Name = "YourCookieName"; options.Cookie.SameSite = SameSiteMode.None; options.Events.OnRedirectToLogin = context => { context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; return Task.CompletedTask; }; });

这将为您创建一个Cookie身份验证方案,并将其添加到应用程序中。

然后,在您的控制器或操作方法中,您可以设置Cookie来进行身份验证,并检查Cookie以确保用户获得了授权。

例如,您可以使用以下代码来设置Cookie:

var claims = new List { new Claim(ClaimTypes.Name, "User1"), new Claim(ClaimTypes.Role, "admin") }; var claimsIdentity = new ClaimsIdentity( claims, CookieAuthenticationDefaults.AuthenticationScheme);

await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity));

这将为名为'User1”的用户创建一个Cookie,并将其授予'admin”角色。

然后,在您的IFrame中,您可以使用JavaScript从父级应用程序中访问Cookie。您可以使用以下代码从Cookie中获取身份验证令牌:

function getToken() { var token = document.cookie .split('; ') .find(row => row.startsWith('YourCookieName=')) .split('=')[1]; return token; }

您可以在IFrame中使用此函数,以便您可以从父级应用程序中获取身份验证令牌,以便您可以在您的IFrame中进行授权API调用。

最后,在您的Startup.cs文件中的Configure方法中,您可以添加以下代码来确保ASP.net Core应用程序不会将用户重定向到登录页:

app.Use(async (context, next) => { var authScheme = CookieAuthenticationDefaults.AuthenticationScheme; var user = context.User; if (user?.Identity?.IsAuthenticated ?? false) { await next(); } else if (context.Request.Path.StartsWithSegments("/api")) { context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; } else

相关内容

热门资讯

WPK多开教程书!wepoke... WPK多开教程书!wepoker有透视底牌吗,德州微扑克好像是真的有辅助器攻略暗藏猫腻,小编详细说明...
WPK俱乐部方案!hhpoke... WPK俱乐部方案!hhpoker辅助软件下载,德州微扑克确实存在有透视技巧1、不需要AI权限,帮助你...
WPK有透视法子!we-pok... WPK有透视法子!we-poker辅助器,德州微扑克一直真的有辅助插件1、we-poker辅助器有没...
WPK官网要领!wepoker... WPK官网要领!wepoker公共底牌,德州微扑克其实是真的有透视脚本wepoker公共底牌脚本下载...
WPK俱乐部绝活!wepoke... WPK俱乐部绝活!wepoker有辅助功能吗,德州微扑克一贯是有开挂软件1、wepoker有辅助功能...
WPK多开绝活儿!wejoke... 您好,wejoker免费脚本这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】...
WPK德州局教材!wepoke... WPK德州局教材!wepoker有辅助功能吗,德州微扑克好像真的是有辅助软件所有人都在同一条线上,像...
WPK购买烘培!aapoker... WPK购买烘培!aapoker透视插件下载,德州微扑克确实存在有辅助软件1、任何aapoker透视插...
WPK安装阶段!wepoker... WPK安装阶段!wepoker透视脚本免费,德州微扑克本来是真的有辅助器软件1、金币登录送、破产送、...
WPK安装模板!wepoker... WPK安装模板!wepoker怎么看牌型,德州微扑克其实是真的有透视挂攻略进入游戏-大厅左侧-新手福...