【问题标题】:ASP.NET webforms and MVC authentication sharing via cookie通过 cookie 共享 ASP.NET 网络表单和 MVC 身份验证
【发布时间】:2015-02-25 07:38:46
【问题描述】:

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

问题

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

测试用例

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

[Authorize] public ActionResult About()

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

期待

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

配置

第1步

我使用here 概述的方法从 W1 中提取了validationKey 和decryptionKey。虽然这似乎是一个合乎逻辑的步骤,但我不确定是否需要,因为不同的密钥仍然允许用户登录 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 关于页面的链接时,用户未获得授权并显示登录屏幕。

我在配置中有什么遗漏吗?

【问题讨论】:

    标签: asp.net forms-authentication machinekey


    【解决方案1】:

    终于搞定了!

    1) 将 localhost 或 .localhost 设置为域时无法在本地工作

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

    3)在W1中,需要在AppSettings节点(标签)中添加&lt;add key="ValidationSettings:UnobtrusiveValidationMode" value="None" /&gt;

    希望我花时间发布这个问题和答案对某人有所帮助。我在很多帖子中都找到了这个解决方案的一部分,但这将它们整合在一起。

    【讨论】:

    • 没有什么比偶然发现一篇完美描述您当前情况的三年前的 SO 文章更令人兴奋的了。感谢您花时间记录这一点。
    • 这很有价值,因为我遇到了同样的问题,只是缺少第二个和第三个,谢谢
    猜你喜欢
    • 2018-09-07
    • 1970-01-01
    • 2011-04-22
    • 2018-02-17
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 2014-04-01
    相关资源
    最近更新 更多