首页 > 解决方案 > 更换 PC 时的无限循环/重定向简单的 ASP.NET OWIN Azure AD Web 解决方案

问题描述

我有一个非常简单的 ASP.NET Web 解决方案 (WebForms),它通过 OWIN 实现了 Azure AD 身份验证的样板文件。在我的 PC 上进行本地测试时,它工作正常。我真的在我的 Windows 笔记本电脑上跳了起来,从 Visual Studio Team Services (GIT) 中下载了解决方案,并在我的笔记本电脑上进行了调试。现在,当我尝试登录时,应用程序进入以下两个 Web 请求(一个 POST 一个 GET)的无限循环。我不知道这里发生了什么。有人可以帮忙吗?这两个日志条目只是一遍又一遍地出现,直到我退出。

> Application Insights Telemetry (unconfigured):
> {"name":"Microsoft.ApplicationInsights.Dev.Request","time":"2018-07-09T09:44:50.8000358Z","tags":{"ai.internal.sdkVersion":"web:2.2.0-738","ai.operation.id":"q2LZmfksgJg=","ai.location.ip":"::1","ai.cloud.roleInstance":"DESKTOP-8S777RV","ai.operation.name":"POST
> /"},"data":{"baseType":"RequestData","baseData":{"ver":2,"id":"q2LZmfksgJg=","name":"POST
> /","duration":"00:00:00.0070000","success":true,"responseCode":"302","url":"https://localhost:44378/","properties":{"DeveloperMode":"true"}}}}
> 
> 
> Application Insights Telemetry (unconfigured):
> {"name":"Microsoft.ApplicationInsights.Dev.Request","time":"2018-07-09T09:44:50.8189012Z","tags":{"ai.internal.sdkVersion":"web:2.2.0-738","ai.operation.id":"tDssyCSeipU=","ai.location.ip":"::1","ai.cloud.roleInstance":"DESKTOP-8S777RV","ai.operation.name":"GET
> /ScratchPad.aspx"},"data":{"baseType":"RequestData","baseData":{"ver":2,"id":"tDssyCSeipU=","name":"GET
> /ScratchPad.aspx","duration":"00:00:00.0010000","success":true,"responseCode":"401","url":"http://localhost:63907/ScratchPad.aspx","properties":{"DeveloperMode":"true"}}}}

编辑:我是 OWIN / Azure 身份验证的初学者,但我做了一些处理,发现将 CookieSecureOption.Never 添加到我的 Cookie 身份验证配置中,例如更改

app.UseCookieAuthentication(new CookieAuthenticationOptions ());

app.UseCookieAuthentication(new CookieAuthenticationOptions { CookieSecure = CookieSecureOption.Never });

调试时使站点登录正常。我知道这不是一个安全的选择,但我希望这可以帮助某人首先理解这个问题?因为我在一小时前在我的另一台电脑上测试时不需要这个选项?

标签: asp.netazure-active-directoryowin-middleware

解决方案


OWIN/Katana 有一个错误,会导致您的 cookie 消失。因此,您的解决方案将在 90% 的时间内工作,但有 10% 的时间会陷入这些无限循环。重新部署应用程序可能会暂时修复它,但问题仍然存在。

解决此问题的一种方法是使用 Nuget 包 kentor.owincookiesaver。您应该在 owin 启动类中的 cookieauthentication 调用之前调用此类,如下所示。

app.UseKentorOwinCookieSaver();

app.UseCookieAuthentication(new CookieAuthenticationOptions());

该错误记录在此处:https ://github.com/aspnet/AspNetKatana/ ,解决方法记录在此处:https ://github.com/Sustainsys/owin-cookie-saver

Nuget 包已被下载 177,000 多次。


推荐阅读