ASP. NET Core 2.0 multiple applications implement Cookie sharing and corecookie

Source: Internet
Author: User

ASP. NET Core 2.0 multiple applications implement Cookie sharing and corecookie
Preface

. After NET Core 2.0 was released, there were a lot of changes to the relevant APIs in the Authentication middleware section (official documentation). This article mainly focuses on the implementation of application Cookie sharing, if you are not familiar with the use of Cookie middleware, you can view the official documentation. I hope this article will help you.

ASP. NET Core 2.0 Data Protection)

In some scenarios that require load, Cookie sharing, and encryption and decryption of cookies, we need to implement some methods for Distributed configuration of Data Protection. This article mainly introduces the use of IXmlRepository Interface.

The IXmlRepository interface mainly provides persistence and XML retrieval methods. It only provides two APIs:

// Retrieve all encrypted xml files

IReadOnlyCollection <XElement> GetAllElements ();

 

// Store data protection xml

Void StoreElement (XElement element, string friendlyName );

IXmlRepository custom implementation

Note: Here I will simply read the Key file of the application root directory. You can customize the storage to DB or Redis.

Public class XmlRepository: IXmlRepository {private readonly string _ KeyContentPath = ""; public XmlRepository () {_ KeyContentPath = Path. combine (Directory. getCurrentDirectory (), "inclukeys", "key. xml ");} public IReadOnlyCollection <XElement> GetAllElements () {// load the key information var elements = new List <XElement> () {XElement. load (_ KeyContentPath)}; return elements;} public void StoreElement (XElement element, string friendlyName) {// This document ignores the stored functions, because we only need to read the existing Key }}

Note: The storage location and content of the Key file under each application site must be consistent. The Key in this article is placed in the "Keys keys" folder under the application root directory, and the file is key. xml

ASP. NET Core 2.0 for Cookie sharing

Cookie sharing is divided into application deployment on the same machine and application deployment on different machines or Docker

The application is deployed on the same machine.

This deployment implements Cookie sharing. You only need to set the Data Protection middleware PersistKeys in each application in the same way. We recommend using PersistKeysToFileSystem.

 

Services. addDataProtection (). setApplicationName ("cookieshare") // This method can be used in windows, Linux, and macOS to save it to the file system. persistKeysToFileSystem (new System. IO. directoryInfo ("C: \ share_keys "));

 

The application is deployed on the same machine or Docker

To implement Cookie sharing through such deployment, we need to use the XmlRepository implemented above to set the Cookie. The specific method is as follows.

Services. AddDataProtection (). SetApplicationName ("cookieshare"). AddKeyManagementOptions (options >{// configure custom XmlRepository options. XmlRepository = new XmlRepository ();});
Make. NET Great Again

Although there are some changes to the ASP. net core 2.0 API, I think it is getting better and better. There are always some things that need to be changed. We need to adapt and strive to improve the entire community environment, instead of complaining. If you find it useful, please give me a thumbs up. Your recognition is my greatest motivation.

Example source code: https://github.com/myloveCc/CookieShare

References

Http://www.cnblogs.com/savorboard/p/dotnetcore-data-protected-farm.html

Http://www.cnblogs.com/dudu/p/6495951.html

Https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie? Tabs = aspnetcore1x

Https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview? Tabs = aspnetcore1x

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.