【问题标题】:Why Request.IsAuthenticated is false when token generated from iframe for SSO azure B2C msal .net当从 iframe 为 SSO azure B2C msal .net 生成令牌时,为什么 Request.IsAuthenticated 为假
【发布时间】:2021-09-29 16:38:43
【问题描述】:

我有 2 个应用程序,一个是带有 Msal .net 代码的 mvc Web 应用程序,用于 Azure 广告 B2C 登录,第二个应用程序是使用相同的 azure ad b2c 登录的电源应用程序门户。如果用户登录任何一个应用程序,我已将 iframe 添加到两个应用程序中以进行静默登录,在电源应用程序门户上它可以在所有浏览器(如 chrome、firefox、edge、opera)上运行。但是对于 mvc 应用程序,它只能在 firefox 浏览器上运行,除了 firefox 之外,它的请求不会得到身份验证。当我调试 iframe 请求时,能够看到获取令牌

IConfidentialClientApplication confidentialClient = MsalAppBuilder.BuildConfidentialClientApplication(new ClaimsPrincipal(notification.AuthenticationTicket.Identity));
Globals.ClientInfo = confidentialClient;
// Upon successful sign in, get & cache a token using MSAL
AuthenticationResult result = await confidentialClient.AcquireTokenByAuthorizationCode(Globals.Scopes, notification.Code).ExecuteAsync();
Globals.AuthenticationResult = result;
Globals.Claims = notification.AuthenticationTicket.Identity.Claims;
Globals.EncryptedClaim = notification.JwtSecurityToken.RawData;

但是进入回调函数Request.IsAuthenticated是假的。

我的 Web 应用程序使用 .net framework 4.7.1 和 Microsoft.Identity.Client 版本 4.29.0,请帮我解决这个问题。
此外,我在 chrome 和 edge 上的 b2c 回调方法也出现错误,自动响应 cookie 正在设置 Lax 而不是 None,这是否会导致为 iframe 设置 cookie 的问题。

谢谢,
桑迪

【问题讨论】:

    标签: asp.net-mvc owin azure-ad-b2c


    【解决方案1】:

    Request.IsAuthenticated 在处理 <authentication mode="Forms"> 时为 FALSE,如果处理 <authentication mode="Windows" />,则为 TRUE。

    因此,要解决此问题,请检查 web.config 中的身份验证模式。在您的 web.config 中添加以下代码:

    <authentication mode="Forms">
      <forms loginUrl="~/_Login/Login" timeout="30" />
    </authentication>
    

    <system.webServer>
      <modules>
         <remove name="FormsAuthentication" />
         <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
      </modules>
    <system.webServer>
    

    在注册和配置中将回复 URL 设置为您的主页 URL,并确保应用程序 ID 和客户端 ID 匹配。

    更多信息请参考以下链接:

    如果您在成功登录后被重定向到登录页面,那么您需要更改 CookieManger :

    var cookieOptions = new CookieAuthenticationOptions();
    cookieOptions.CookieManager = new SystemWebCookieManager(); 
    app.UseCookieAuthentication(cookieOptions);
    

    查看此document 了解更多信息。

    【讨论】:

    • 我使用 msal .net 创建了新的示例应用程序,并添加了您的提及代码,但仍在 chrome iframe 上请求未在 chrome 上获得身份验证
    • 您是在登录后遇到令牌超时错误还是被重定向回登录页面,您能否详细说明问题以便我提供帮助。
    • 当请求从 iframe 发送以进行静默登录时,在回调函数 Adlogincallback() 中,正如我在我的主要帖子图片中提到的,Request.IsAutheticanted 对于 chrome 来说是 False,没有异常或超时消息即将到来,当在回调函数标志中从 firefox 尝试相同时为 True。
    • 您能否清除缓存并重试。如果您的代码中有CookieSecure = CookieSecureOption.Always,请尝试对其进行评论并检查。还要检查在 web.config 中添加这个 &lt;system.webServer&gt; &lt;modules runAllManagedModulesForAllRequests="true"&gt;&lt;/modules&gt; &lt;/system.webServer&gt; 是否有效。您的应用程序在登录然后刷新页面后是否正常工作?
    • 添加了你提到的 web.config 设置仍然不起作用。实际上,除了正常登录工作之外,我只对 iframe 请求有问题。我想实现用户从 Power Apps 应用程序登录,通过 iframe 我将向登录方法发送请求,以检查登录后 iframe 是否加载主页然后重新加载页面,如果没有,则将用户保留在登录页面上进行登录。
    猜你喜欢
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 2020-10-10
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 2021-08-17
    相关资源
    最近更新 更多