This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#105 Use DataProtection to encrypt the cookie
- Loading branch information
Showing
9 changed files
with
127 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Text; | ||
using Microsoft.AspNetCore.DataProtection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.AspNetCore.Session | ||
{ | ||
internal static class CookieProtection | ||
{ | ||
internal static string Protect(IDataProtector protector, string data) | ||
{ | ||
if (protector == null) | ||
{ | ||
throw new ArgumentNullException(nameof(protector)); | ||
} | ||
if (string.IsNullOrEmpty(data)) | ||
{ | ||
return data; | ||
} | ||
|
||
var userData = Encoding.UTF8.GetBytes(data); | ||
|
||
var protectedData = protector.Protect(userData); | ||
return Convert.ToBase64String(protectedData).TrimEnd('='); | ||
} | ||
|
||
internal static string Unprotect(IDataProtector protector, string protectedText, ILogger logger) | ||
{ | ||
try | ||
{ | ||
if (string.IsNullOrEmpty(protectedText)) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
var protectedData = Convert.FromBase64String(Pad(protectedText)); | ||
if (protectedData == null) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
var userData = protector.Unprotect(protectedData); | ||
if (userData == null) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
return Encoding.UTF8.GetString(userData); | ||
} | ||
catch (Exception ex) | ||
{ | ||
// Log the exception, but do not leak other information | ||
logger.ErrorUnprotectingSessionCookie(ex); | ||
return string.Empty; | ||
} | ||
} | ||
|
||
private static string Pad(string text) | ||
{ | ||
var padding = 3 - ((text.Length + 3) % 4); | ||
if (padding == 0) | ||
{ | ||
return text; | ||
} | ||
return text + new string('=', padding); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters