2016-05-07 38 views
2

我有幾個MVC 5應用程序共享相同的身份驗證Cookie。我正在使用ASP.NET身份來創建cookie。在Asp.Net Core 1(MVC6)和MVC 5應用程序之間共享身份驗證Cookie

我檢查,如果用戶使用的是Owin的helper方法驗證,像這樣:

app.UseCookieAuthentication(
    new CookieAuthenticationOptions 
    { 
     AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
     ExpireTimeSpan = TimeSpan.FromMinutes(expirationTimeInMinutes), 
     LoginPath = new PathString("/Account/Login"), 
     Provider = new CookieAuthenticationProvider() 
    }); 

而在所有應用程序中使用這個cookie,我在web.config文件中的以下配置:

<machineKey validationKey="..." decryptionKey="..." validation="SHA1" /> 

據我所知,這種配置允許應用程序解密相同的cookie。

在MVC6應用程序,我將它設置爲使用的cookies是這樣的:

app.UseCookieAuthentication(options => 
    { 
    //options.AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie; 
    options.ExpireTimeSpan = TimeSpan.FromMinutes(20); 
    options.LoginPath = new PathString("/Account/login"); 
    //options.Provider = new CookieAuthenticationProvider() 
    }); 

OK,這裏是我的問題的配置已經是不同的,因爲我不知道指定供應商,也不認證類型。

之後,我必須配置解密密鑰,但據我所知,MVC 6沒有web.config文件。那麼我怎麼能做到這一點呢?

+0

這兩個網站是否在同一個域名中? – Win

+0

是的,他們在同一個域中。 – vintem

回答

0

我強烈建議轉移到OAuth 2.0或OpenID Connect以在多個應用程序之間進行一次身份驗證,但除此之外,您希望執行的操作是查看Data Protection API。

爲了獲得與API使用配置值,而不是web.config你可以使用下列方法之一:

  • 一個.json文件或部署爲自動部署的一部分一些其他類型的文件進程並在您的應用啓動時有條件地加載
  • 服務器上配置的環境變量,可能位於該應用程序池的IIS的applicationHost.config(需要IIS 10.0或更高版本的應用程序池環境變量,或者如果您使用的是HttpPlatformHandler/AspNetCoreModule可以爲此配置環境變量)
  • 其他任何你的想象力能想出

您可以在此文檔頁面閱讀更多關於配置可能的選項:

https://docs.asp.net/en/latest/fundamentals/configuration.html

3

免責聲明:這個答案是適用於RC2只,這應該在5月中旬發佈。它可能適用於RC1,但需要更多的工作。


您可以使用新的Microsoft.Owin.Security.Interop包,使OWIN /武士刀餅乾中間件使用由ASP.NET核心(周圍的其他方式使用新的序列化格式和新的數據保護堆棧會更難,和絕對不推薦):

OWIN /卡塔納應用

public class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     // Create a new data protection provider with a unique app 
     // name shared by both your OWIN/Katana and ASP.NET Core apps: 
     var provider = DataProtectionProvider.Create("your app name"); 

     // Create a protector compatible with the ASP.NET Core cookies middleware. 
     // Replace the second argument ("Cookies") by the authentication scheme 
     // used by your ASP.NET Core cookies middleware if necessary. 
     var protector = provider.CreateProtector(
      "Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware", 
      "Cookies", "v2"); 

     // Set TicketDataFormat to force the OWIN/Katana cookies middleware 
     // to use the new serialization format used by ASP.NET Core: 
     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      TicketDataFormat = new AspNetTicketDataFormat(new DataProtectorShim(protector)) 
     }); 
    } 
} 

ASP.NET核心應用

public class Startup 
{ 
    public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddDataProtection(options => 
     { 
      // Force the ASP.NET Core data protection stack to use 
      // the name shared with your OWIN/Katana app. 
      options.ApplicationDiscriminator = "your app name"; 
     }); 
    } 
} 

你應該可以,如果你只使用它的餅乾中間件以消除你的web.configmachineKey節點,作爲OWIN /武士刀餅乾中間件現在將使用新的數據保護堆棧,不依賴於機器密鑰,但在機器上保留一個密鑰環(默認情況下,在註冊表中或在特殊文件夾中)。

如果您的應用程序部署在不同的機器上,我建議您同步機器上的鑰匙環。你可以閱讀this other SO post瞭解更多信息。

+0

有沒有我可以試試這個每晚構建? – vintem

+0

您可以使用aspnetcirelease提要試用RC2版本,但請注意VS不會很好地使用這些新軟件包(至少在新版CLI工具發佈之前):https://www.myget。 org/gallery/aspnetcirelease – Pinpoint

+0

謝謝。目前,我需要的是新的AspNetCore將與我們現有的應用程序一起工作的POC。 – vintem

0

我得到了ASP.NET核心側的簡單的解決方案(針對我的情況) - 無需修改ASP.NET 4方面:

我剛剛建立了一個簡單的IDataProtector(像上面的墊片),其在解除保護方法中執行以下操作:

public byte[] Unprotect(byte[] protectedData) 
{ 
    // ... call the Unprotect method of the "shimmed" protector, 
    // you'll get back the bytes 

    // now make the Owin serializer to deserialize the old format 

    Microsoft.Owin.Security.DataHandler.Serializer.TicketSerializer owinSerializer = 
      new Microsoft.Owin.Security.DataHandler.Serializer.TicketSerializer(); 

    Microsoft.Owin.Security.AuthenticationTicket owinTicket = owinSerializer.Deserialize(cookie); 

    // now build the ticket for the new format 

    Microsoft.AspNetCore.Authentication.AuthenticationTicket coreTicket = new 
      Microsoft.AspNetCore.Authentication.AuthenticationTicket(
      new System.Security.Claims.ClaimsPrincipal(owinTicket.Identity), 
      new Microsoft.AspNetCore.Http.Authentication.AuthenticationProperties(owinTicket.Properties.Dictionary), 
      _purpose); 

    Microsoft.AspNetCore.Authentication.TicketSerializer coreSerializer = 
     new Microsoft.AspNetCore.Authentication.TicketSerializer(); 

    // and return the new format, serialized - this will work 

    return coreSerializer.Serialize(coreTicket); 
} 
相關問題