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

时间:2015-02-25 07:38:47

标签: 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中添加了一个指向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为目标的链接时,该用户未获得授权,并显示登录屏幕。

配置中是否缺少某些内容?

1 个答案:

答案 0 :(得分:3)

终于让这个工作了!

1)无法在本地使用localhost或.localhost设置为域

2)在W1中,需要添加属性targetFramework =&#34; 4.5&#34;到httpRuntime

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

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