Asp.net 由同一域中的两个应用程序共享表单身份验证

Asp.net share forms authentication by two applications in the same domain

我在同一域中有两个应用程序:mydomain/app1 和 mydomain/app2,我需要在它们之间共享身份验证。一个应用程序使用 Asp.Net WebForms,第二个应用程序使用 WebApi2。

我配置 web.config 如下:

应用程序 1:

<authentication mode="Forms">
  <forms loginUrl="/Login.aspx" defaultUrl="/Default.aspx" name=".ASPXFORMSAUTH" protection="All" cookieless="UseDeviceProfile" slidingExpiration="true" path="/" domain="mydomain" requireSSL="false" timeout="60" enableCrossAppRedirects="false">
  </forms>
</authentication>
<authorization>
  <deny users="?" />
  <allow users="*"/>
</authorization>
<machineKey validationKey="generated key1" decryptionKey="generated key2" validation="SHA1"/>

应用程序 2:

<authentication mode="Forms">
  <forms loginUrl="/index.html" defaultUrl="/index.html" name=".ASPXFORMSAUTH" protection="All" cookieless="UseDeviceProfile" slidingExpiration="true" path="/" domain="mydomain" requireSSL="false" timeout="60" enableCrossAppRedirects="false">
  </forms>
</authentication>
<authorization>
  <deny users="?" />
  <allow users="*"/>
</authorization>
<machineKey validationKey="generated key1" decryptionKey="generated key2" validation="SHA1"/>

我正在登录 app1 并观察 cookie 内容。 然后我在另一个浏览器选项卡中从 app2 请求 URL。第二个选项卡中的 cookie(名称和内容)与第一个选项卡中的相同。 我希望来自第二个选项卡的请求由 app2 进行身份验证,因为 cookie 已经由 app1 进行了身份验证。 然而,这并没有发生,我被重定向到 app2 的登录页面。

按照 OP 的建议,这里是适用于这种情况的答案。

他们在 4 和 4.5 之间更改了 cookie 加密。您可以将两者 运行 置于同一个 .net 下,或者通过向您的机器密钥配置节点添加属性来打开 4.5 站点上的兼容性。

https://social.microsoft.com/Forums/en-US/1791c5e3-4087-4e92-a460-51c5c4221f49/any-forms-auth-changes-in-45?forum=Offtopic