我的问题的部分答案似乎散布在多个帖子中,但到目前为止对我来说,将它们放在一起还没有奏效,所以我希望当这篇文章得到回答时,它会形成更完整的指南

问题

我有一个 ASP.NET webforms 应用程序 (W1),我想在一段时间内开始升级到单独的 MVC 应用程序 (M1)。包含 W1 的解决方案已升级到 4.5,并且已在解决方案中创建 M1。 W1 使用 ASP.Net 成员资格框架。

测试用例

在 M1 中,我在 HomeController 的 About 页面中添加了 Authorize 属性
[Authorize]public ActionResult About()
我在 M1 中添加了一个指向关于页面的链接,该链接源自 W1 中需要用户登录的页面。

期待

我希望用户能够登录 W1,单击 M1 关于页面的链接并自动登录 M1。

配置

步骤1

我已经使用 here 概述的方法从 W1 中提取了验证 key 和解密 key 。虽然这似乎是一个合乎逻辑的步骤,但我不确定它是否需要,因为不同的 key 仍然允许用户登录 W1。

第2步

根据 herehere 信息,经过大量调试,我修改了项目的 Web.config 文件部分,如下所示;

对于 W1:

<system.web>
    <authentication mode="Forms">
          <forms name="WRSAUTH"
                 loginUrl="~/Account/Login.aspx"
                 defaultUrl="Default.aspx"
                 protection="All"
                 timeout="60"
                 path="/"
                 domain=".localhost"
                 requireSSL="false"
                 slidingExpiration="true"
                 cookieless="UseCookies"
                 enableCrossAppRedirects="false" />
        </authentication>
        <machineKey validationKey="<ValidationKey>"
                    decryptionKey="<DecryptionKey>"
                    validation="SHA1"
                    decryption="AES"/>
<compilation debug="true" targetFramework="4.5">
     <httpRuntime maxRequestLength="12288" />
</system.web>

对于 M1:
  <system.web>
    <authentication mode="Forms">
      <forms name="WRSAUTH"
             loginUrl="~/Account/Login"
             defaultUrl="~/"
             protection="All"
             timeout="60"
             path="/"
             domain=".localhost"
             requireSSL="false"
             slidingExpiration="true"
             cookieless="UseCookies"
             enableCrossAppRedirects="false"/>
    </authentication>
    <machineKey validationKey="<ValidationKey>"
                decryptionKey="<DecryptionKey>"
                validation="SHA1"
                decryption="AES"/>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.webServer>
    <modules>
      <!--<remove name="FormsAuthentication"/>-->
    </modules>
  </system.webServer>

当前状态

单击 W1 中针对 M1 about 页面的链接时,用户未获得授权并显示登录屏幕。

我在配置中缺少什么吗?

最佳答案

终于让这个工作了!

1) 不能在本地使用 localhost 或 .localhost 设置为域

2)在W1中,需要给httpRuntime添加属性targetFramework="4.5"

3)在W1中,需要在AppSettings节点(标签)中添加<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
希望我花在发布这个问题和答案上的时间对某人有所帮助。我在许多帖子中找到了这个解决方案的部分内容,但这将它们整合在一起。

关于通过 cookie 共享 ASP.NET webforms 和 MVC 身份验证,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28713628/

10-11 20:11