Skip to content

Commit d7b8f35

Browse files
author
Craig Boland
committed
Integrate FormsAuthenticationTicket encryption.
Add usage to test applications.
1 parent 4af62a0 commit d7b8f35

File tree

7 files changed

+86
-45
lines changed

7 files changed

+86
-45
lines changed

samples/TestImplementation.ReadCookie/Controllers/SecurityController.cs

+13
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,28 @@
22
using Microsoft.AspNetCore.Authentication.Cookies;
33
using Microsoft.AspNetCore.Authorization;
44
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.Extensions.Options;
6+
using Synercoding.FormsAuthentication;
57
using System.Threading.Tasks;
68

79
namespace TestImplementation.ReadCookie.Controllers
810
{
911
[Authorize]
1012
public class SecurityController : Controller
1113
{
14+
private readonly FormsAuthenticationOptions _formsAuthenticationOptions;
15+
16+
public SecurityController(IOptions<FormsAuthenticationOptions> options)
17+
{
18+
_formsAuthenticationOptions = options.Value;
19+
}
20+
1221
public IActionResult Index()
1322
{
23+
var authCryptor = new FormsAuthenticationCryptor(_formsAuthenticationOptions);
24+
var ticket = authCryptor.Unprotect(Request.Cookies["TestCookie"]);
25+
ViewData["TestCookie-UserData"] = ticket.UserData;
26+
1427
return View();
1528
}
1629

samples/TestImplementation.ReadCookie/Startup.cs

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public void ConfigureServices(IServiceCollection services)
3131
ValidationMethod = section.GetValue<ValidationMethod>("ValidationMethod"),
3232
};
3333

34+
// Enables injection of IOptions<FormsAuthenticationOptions>
35+
services.Configure<FormsAuthenticationOptions>(section);
36+
3437
services.AddAuthentication(options =>
3538
{
3639
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;

samples/TestImplementation.ReadCookie/Views/Security/Index.cshtml

+4-1
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,7 @@
5454
</tr>
5555
}
5656
</tbody>
57-
</table>
57+
</table>
58+
59+
<h3>Ticket User Data</h3>
60+
<div>@ViewData["TestCookie-UserData"]</div>

samples/TestImplementation.SetCookie/Controllers/LoginController.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Web.Mvc;
1+
using System;
2+
using System.Web;
3+
using System.Web.Mvc;
24
using TestImplementation.SetCookie.Models;
35

46
namespace TestImplementation.SetCookie.Controllers
@@ -19,6 +21,10 @@ public ActionResult Index(LoginVM model)
1921
{
2022
if (ModelState.IsValid)
2123
{
24+
var ticket = new System.Web.Security.FormsAuthenticationTicket(1, "TestTicket", DateTime.Now, DateTime.Now.AddDays(1), true, "The answer is '42'.");
25+
var encryptedTicket = System.Web.Security.FormsAuthentication.Encrypt(ticket);
26+
Response.Cookies.Add(new HttpCookie("TestCookie", encryptedTicket));
27+
2228
System.Web.Security.FormsAuthentication.SetAuthCookie(model.UserName, true);
2329
return Redirect(model.ReturnUrl);
2430
}

src/Synercoding.FormsAuthentication/FormsAuthenticationCryptor.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ public string Protect(FormsAuthenticationCookie cookie)
3333
return CryptoUtil.BinaryToHex(protectedData);
3434
}
3535

36+
public string Protect(FormsAuthenticationTicket ticket)
37+
{
38+
if (ticket == null)
39+
throw new ArgumentNullException(nameof(ticket));
40+
41+
var unprotectedData = FormsAuthenticationTicketSerializer.Serialize(ticket);
42+
43+
var cryptoProvider = AspNetCryptoServiceProvider.GetCryptoServiceProvider(_options);
44+
var cryptoService = cryptoProvider.GetCryptoService();
45+
byte[] protectedData = cryptoService.Protect(unprotectedData);
46+
47+
return CryptoUtil.BinaryToHex(protectedData);
48+
}
49+
3650
public FormsAuthenticationCookie Unprotect(string protectedText)
3751
{
3852
if (protectedText == null)
@@ -99,7 +113,7 @@ private FormsAuthenticationCookie ConvertToAuthenticationTicket(byte[] data)
99113
byte footer = ticketReader.ReadByte();
100114
if (footer != 0xFF)
101115
throw new ArgumentException("The data is not in the correct format, footer byte must be 0xFF.", nameof(data));
102-
116+
103117
//create ticket
104118
return new FormsAuthenticationCookie()
105119
{

src/Synercoding.FormsAuthentication/FormsAuthenticationTicket.cs

+41-40
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
* Copyright (c) 1999 Microsoft Corporation
1111
*/
1212

13-
namespace System.Web.Security
13+
namespace Synercoding.FormsAuthentication
1414
{
1515
using System.Security.Principal;
1616
using System.Security.Permissions;
17-
using System.Web.Configuration;
17+
//using System.Web.Configuration;
1818
using System.Runtime.Serialization;
19+
using System;
1920

2021

2122
/// <devdoc>
@@ -174,25 +175,25 @@ internal DateTime IssueDateUtc
174175
private DateTime _IssueDateUtc;
175176

176177

177-
/// <devdoc>
178-
/// <para>This constructor creates a
179-
/// FormsAuthenticationTicket instance with explicit values.</para>
180-
/// </devdoc>
181-
public FormsAuthenticationTicket(int version,
182-
String name,
183-
DateTime issueDate,
184-
DateTime expiration,
185-
bool isPersistent,
186-
String userData)
187-
{
188-
_Version = version;
189-
_Name = name;
190-
_Expiration = expiration;
191-
_IssueDate = issueDate;
192-
_IsPersistent = isPersistent;
193-
_UserData = userData;
194-
_CookiePath = FormsAuthentication.FormsCookiePath;
195-
}
178+
///// <devdoc>
179+
///// <para>This constructor creates a
180+
///// FormsAuthenticationTicket instance with explicit values.</para>
181+
///// </devdoc>
182+
//public FormsAuthenticationTicket(int version,
183+
// String name,
184+
// DateTime issueDate,
185+
// DateTime expiration,
186+
// bool isPersistent,
187+
// String userData)
188+
//{
189+
// _Version = version;
190+
// _Name = name;
191+
// _Expiration = expiration;
192+
// _IssueDate = issueDate;
193+
// _IsPersistent = isPersistent;
194+
// _UserData = userData;
195+
// _CookiePath = FormsAuthentication.FormsCookiePath;
196+
//}
196197

197198

198199
public FormsAuthenticationTicket(int version,
@@ -214,25 +215,25 @@ public FormsAuthenticationTicket(int version,
214215

215216

216217

217-
/// <devdoc>
218-
/// <para> This constructor creates
219-
/// a FormsAuthenticationTicket instance with the specified name and cookie durability,
220-
/// and default values for the other settings.</para>
221-
/// </devdoc>
222-
public FormsAuthenticationTicket(String name, bool isPersistent, Int32 timeout)
223-
{
224-
_Version = 2;
225-
_Name = name;
226-
_IssueDateUtcHasValue = true;
227-
_IssueDateUtc = DateTime.UtcNow;
228-
_IssueDate = DateTime.Now;
229-
_IsPersistent = isPersistent;
230-
_UserData = "";
231-
_ExpirationUtcHasValue = true;
232-
_ExpirationUtc = _IssueDateUtc.AddMinutes(timeout);
233-
_Expiration = _IssueDate.AddMinutes(timeout);
234-
_CookiePath = FormsAuthentication.FormsCookiePath;
235-
}
218+
///// <devdoc>
219+
///// <para> This constructor creates
220+
///// a FormsAuthenticationTicket instance with the specified name and cookie durability,
221+
///// and default values for the other settings.</para>
222+
///// </devdoc>
223+
//public FormsAuthenticationTicket(String name, bool isPersistent, Int32 timeout)
224+
//{
225+
// _Version = 2;
226+
// _Name = name;
227+
// _IssueDateUtcHasValue = true;
228+
// _IssueDateUtc = DateTime.UtcNow;
229+
// _IssueDate = DateTime.Now;
230+
// _IsPersistent = isPersistent;
231+
// _UserData = "";
232+
// _ExpirationUtcHasValue = true;
233+
// _ExpirationUtc = _IssueDateUtc.AddMinutes(timeout);
234+
// _Expiration = _IssueDate.AddMinutes(timeout);
235+
// _CookiePath = FormsAuthentication.FormsCookiePath;
236+
//}
236237

237238
internal static FormsAuthenticationTicket FromUtc(int version, String name, DateTime issueDateUtc, DateTime expirationUtc, bool isPersistent, String userData, String cookiePath)
238239
{

src/Synercoding.FormsAuthentication/FormsAuthenticationTicketSerializer.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
// </copyright>
55
//------------------------------------------------------------------------------
66

7-
namespace System.Web.Security
7+
namespace Synercoding.FormsAuthentication
88
{
99
using System;
10+
using System.Diagnostics;
1011
using System.IO;
1112
using System.Security.Cryptography;
1213
using System.Text;
13-
using System.Web.Util;
14+
//using System.Web.Util;
1415

1516
// A helper class which can serialize / deserialize FormsAuthenticationTicket instances.
1617
//

0 commit comments

Comments
 (0)