12 KiB
title | author | description | ms.author | ms.custom | ms.date | uid |
---|---|---|---|---|---|---|
Share cookies among apps with ASP.NET and ASP.NET Core | rick-anderson | Learn how to share authentication cookies among ASP.NET 4.x and ASP.NET Core apps. | riande | mvc | 01/19/2017 | security/cookie-sharing |
Share cookies among apps with ASP.NET and ASP.NET Core
By Rick Anderson and Luke Latham
Websites often consist of individual web apps working together. To provide a single sign-on (SSO) experience, web apps within a site must share authentication cookies. To support this scenario, the data protection stack allows sharing Katana cookie authentication and ASP.NET Core cookie authentication tickets.
View or download sample code (how to download)
The sample illustrates cookie sharing across three apps that use cookie authentication:
- ASP.NET Core 2.0 Razor Pages app without using ASP.NET Core Identity
- ASP.NET Core 2.0 MVC app with ASP.NET Core Identity
- ASP.NET Framework 4.6.1 MVC app with ASP.NET Identity
In the examples that follow:
- The authentication cookie name is set to a common value of
.AspNet.SharedCookie
. - The
AuthenticationType
is set toIdentity.Application
either explicitly or by default. - A common app name is used to enable the data protection system to share data protection keys (
SharedCookieApp
). Identity.Application
is used as the authentication scheme. Whatever scheme is used, it must be used consistently within and across the shared cookie apps either as the default scheme or by explicitly setting it. The scheme is used when encrypting and decrypting cookies, so a consistent scheme must be used across apps.- A common data protection key storage location is used. The sample app uses a folder named KeyRing at the root of the solution to hold the data protection keys.
- In the ASP.NET Core apps, PersistKeysToFileSystem is used to set the key storage location. SetApplicationName is used to configure a common shared app name.
- In the .NET Framework app, the cookie authentication middleware uses an implementation of DataProtectionProvider.
DataProtectionProvider
provides data protection services for the encryption and decryption of authentication cookie payload data. TheDataProtectionProvider
instance is isolated from the data protection system used by other parts of the app.- DataProtectionProvider.Create(System.IO.DirectoryInfo, Action<IDataProtectionBuilder>) accepts a DirectoryInfo to specify the location for data protection key storage. The sample app provides the path of the KeyRing folder to
DirectoryInfo
. DataProtectionBuilderExtensions.SetApplicationName sets the common app name. - DataProtectionProvider requires the Microsoft.AspNetCore.DataProtection.Extensions NuGet package. To obtain this package for ASP.NET Core 2.1 and later apps, reference the Microsoft.AspNetCore.App metapackage. When targeting the .NET Framework, add a package reference to
Microsoft.AspNetCore.DataProtection.Extensions
.
- DataProtectionProvider.Create(System.IO.DirectoryInfo, Action<IDataProtectionBuilder>) accepts a DirectoryInfo to specify the location for data protection key storage. The sample app provides the path of the KeyRing folder to
Share authentication cookies among ASP.NET Core apps
When using ASP.NET Core Identity:
::: moniker range=">= aspnetcore-2.0"
In the ConfigureServices
method, use the ConfigureApplicationCookie extension method to set up the data protection service for cookies.
Data protection keys and the app name must be shared among apps. In the sample apps, GetKeyRingDirInfo
returns the common key storage location to the PersistKeysToFileSystem method. Use SetApplicationName to configure a common shared app name (SharedCookieApp
in the sample). For more information, see Configuring Data Protection.
When hosting apps that share cookies across subdomains, specify a common domain in the Cookie.Domain property. To share cookies across apps at contoso.com
, such as first_subdomain.contoso.com
and second_subdomain.contoso.com
, specify the Cookie.Domain
as .contoso.com
:
options.Cookie.Domain = ".contoso.com";
See the CookieAuthWithIdentity.Core project in the sample code (how to download).
::: moniker-end
::: moniker range="< aspnetcore-2.0"
In the Configure
method, use the CookieAuthenticationOptions to set up:
- The data protection service for cookies.
- The
AuthenticationScheme
to match ASP.NET 4.x.
app.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Cookies.ApplicationCookie.AuthenticationScheme =
"ApplicationCookie";
var protectionProvider =
DataProtectionProvider.Create(
new DirectoryInfo(@"PATH_TO_KEY_RING_FOLDER"));
options.Cookies.ApplicationCookie.DataProtectionProvider =
protectionProvider;
options.Cookies.ApplicationCookie.TicketDataFormat =
new TicketDataFormat(protectionProvider.CreateProtector(
"Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware",
"Cookies",
"v2"));
});
::: moniker-end
When using cookies directly:
::: moniker range=">= aspnetcore-2.0"
Data protection keys and the app name must be shared among apps. In the sample apps, GetKeyRingDirInfo
returns the common key storage location to the PersistKeysToFileSystem method. Use SetApplicationName to configure a common shared app name (SharedCookieApp
in the sample). For more information, see Configuring Data Protection.
When hosting apps that share cookies across subdomains, specify a common domain in the Cookie.Domain property. To share cookies across apps at contoso.com
, such as first_subdomain.contoso.com
and second_subdomain.contoso.com
, specify the Cookie.Domain
as .contoso.com
:
options.Cookie.Domain = ".contoso.com";
See the CookieAuth.Core project in the sample code (how to download).
::: moniker-end
::: moniker range="< aspnetcore-2.0"
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
DataProtectionProvider =
DataProtectionProvider.Create(
new DirectoryInfo(@"PATH_TO_KEY_RING_FOLDER"))
});
::: moniker-end
Encrypting data protection keys at rest
For production deployments, configure the DataProtectionProvider
to encrypt keys at rest with DPAPI or an X509Certificate. See Key Encryption At Rest for more information.
::: moniker range=">= aspnetcore-2.0"
services.AddDataProtection()
.ProtectKeysWithCertificate("thumbprint");
::: moniker-end
::: moniker range="< aspnetcore-2.0"
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
DataProtectionProvider = DataProtectionProvider.Create(
new DirectoryInfo(@"PATH_TO_KEY_RING"),
configure =>
{
configure.ProtectKeysWithCertificate("thumbprint");
})
});
::: moniker-end
Sharing authentication cookies between ASP.NET 4.x and ASP.NET Core apps
ASP.NET 4.x apps which use Katana cookie authentication middleware can be configured to generate authentication cookies that are compatible with the ASP.NET Core cookie authentication middleware. This allows upgrading a large site's individual apps piecemeal while providing a smooth SSO experience across the site.
When an app uses Katana cookie authentication middleware, it calls UseCookieAuthentication
in the project's Startup.Auth.cs file. ASP.NET 4.x web app projects created with Visual Studio 2013 and later use the Katana cookie authentication middleware by default. Although UseCookieAuthentication
is obsolete and unsupported for ASP.NET Core apps, calling UseCookieAuthentication
in an ASP.NET 4.x app that uses Katana cookie authentication middleware is valid.
An ASP.NET 4.x app must target .NET Framework 4.5.1 or higher. Otherwise, the necessary NuGet packages fail to install.
To share authentication cookies between an ASP.NET 4.x app and an ASP.NET Core app, configure the ASP.NET Core app as stated above, then configure the ASP.NET 4.x app by following these steps:
-
Install the package Microsoft.Owin.Security.Interop into each ASP.NET 4.x app.
-
In Startup.Auth.cs, locate the call to
UseCookieAuthentication
and modify it as follows. Change the cookie name to match the name used by the ASP.NET Core cookie authentication middleware. Provide an instance of aDataProtectionProvider
initialized to the common data protection key storage location. Make sure that the app name is set to the common app name used by all apps that share cookies,SharedCookieApp
in the sample app.
See the CookieAuthWithIdentity.NETFramework project in the sample code (how to download).
When generating a user identity, the authentication type must match the type defined in AuthenticationType
set with UseCookieAuthentication
.
Models/IdentityModels.cs:
Use a common user database
Confirm that the identity system for each app is pointed at the same user database. Otherwise, the identity system produces failures at runtime when it attempts to match the information in the authentication cookie against the information in its database.