简体   繁体   中英

Can an ASP.NET 4.0 application within an ASP.NET 2.0 site use the same Forms Authentication cookie?

See update at bottom of question

I have an ASP.NET 2.0 web application (say https://mysite.somedomain.com/ ) which uses forms authentication. I wish to integrate an ASP.NET 4.0 web app within this site, based at https://mysite.somedomain.com/NewApp/ . Forms Auth is working on the outer app, but the inner app is rejecting the cookie.

web.config on the outer (ASP.NET 2.0) web app contains:

<httpCookies requireSSL="true"/>
<authentication mode="Forms">
    <forms name="MySiteWebAuth" loginUrl="/Login.aspx" protection="All" 
           path="/" timeout="90" requireSSL="true" slidingExpiration="true"/>  
</authentication>
<machineKey (same machine key is in both configs)  
    validation="SHA1"
    decryption="AES"/>
<authorization>
  <deny users="?"/>
  <allow users="*" />
</authorization>

web.config on the inner (ASP.NET 4.0) web app contains:

<authentication mode="Forms">
    <forms name="MySiteWebAuth" loginUrl="/Login.aspx" protection="All" 
           path="/" timeout="90" requireSSL="true" slidingExpiration="true"
           ticketCompatibilityMode="Framework20"/>  
</authentication>
<machineKey (same machine key is in both configs)  
    validation="SHA1"
    decryption="AES"/>

This is the code in Login.aspx.cs that sets the cookie on successful authentication:

FormsAuthenticationTicket ticket = 
    new FormsAuthenticationTicket(
        1, 
        ApplicationContext.User.Identity.Name, 
        DateTime.Now, 
        DateTime.Now.AddMinutes(90), 
        false, 
        ApplicationContext.User.Identity.SessionID.ToString()
    );
HttpCookie cookie = 
   new HttpCookie(
       FormsAuthentication.FormsCookieName, 
       FormsAuthentication.Encrypt(ticket)
   );

cookie.Path = FormsAuthentication.FormsCookiePath;
cookie.HttpOnly = true;
Response.Cookies.Add(cookie);

If I log into the outer web app, then navigate to a page within the inner web app, it does a redirect to the login page and writes Forms authentication failed for the request. Reason: The ticket supplied was invalid. Forms authentication failed for the request. Reason: The ticket supplied was invalid. to the event log on the server.

How do I get the ASP.NET 2.0 Forms Auth ticket to be accepted by the inner ASP.NET 4.0 web app?

Update : It works under HTTPS IIS 7.5, but not under HTTPS IIS 7.0. Doing some more investigation.

Update 2 : We have applied Server 2008 SP2 to the server along with the recent patch for the hash-collision DoS and since then the cookie sharing has worked.

除了保持几乎所有上述值相同外,还需要在4.0应用程序的配置文件中设置machineKey元素的compatibilityMode属性:

<machineKey compatibilityMode="Framework20SP2" validationKey="THE_KEY" decryptionKey="ANOTHER_KEY" validation="SHA1" />

我假设你读过这个 - http://msdn.microsoft.com/en-us/library/eb0zx8fc.aspx - 也许从你的4.0应用程序中删除ticketCompatibilityMode属性,或者至少确保它们是相同的。

Add this appsetting to both web.config files:

add key="aspnet:UseLegacyFormsAuthenticationTicketCompatibility" value="true"

You need to make sure both applications have the same encryption key in the web.config files

<machineKey
      validationKey="C50B3C89CB21F4F1422FF158A5B42D0E8DB8CB5CDA1742572A487D9401E3400267682B202B746511891C1BAF47F8D25C07F6C39A104696DB51F17C529AD3CABE" 
      decryptionKey="8A9BE8FD67AF6979E7D20198CFEA50DD3D3799C77AF2B72F" 
      validation="SHA1" />

Forms Authentication Across Applications

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM