ASP.NET Webforms 和 MVC 身份验证通过 cookie 共享

Att*_*ers 3 asp.net forms-authentication machinekey

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

问题

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

测试用例

在M1中,我将Authorize属性添加到HomeController中的About页面

[Authorize] public ActionResult About()

我在 M1 中添加了一个指向“关于”页面的链接,该链接源自 W1 中要求用户登录的页面。

期望

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

配置

步骤1

我已使用此处概述的方法从 W1 中提取了validationKey 和decryptionKey 。尽管这似乎是一个合乎逻辑的步骤,但我不确定是否需要这样做,因为不同的密钥仍然允许用户登录 W1。

第2步

按照此处和此处的信息,并经过大量调试后,我修改了项目的 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>
Run Code Online (Sandbox Code Playgroud)

对于 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>
Run Code Online (Sandbox Code Playgroud)

当前状态

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

我的配置中缺少什么吗?

Att*_*ers 5

终于让这个工作了!

1) 无法在本地使用 localhost 或 .localhost 设置为域的情况下工作

2)在W1中,需要将属性targetFramework =“4.5”添加到httpRuntime

<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />3)在W1中,需要在AppSettings节点内添加(标签)

希望我发布这个问题和答案所花费的时间可以帮助别人。我在许多帖子中找到了该解决方案的各个部分,但这将它们全部整合在一起。