2015-02-06 13:47:00 +11:00
|
|
|
|
using System;
|
2015-02-06 16:13:02 +11:00
|
|
|
|
using System.Security.Claims;
|
2015-02-06 13:47:00 +11:00
|
|
|
|
using System.Web.Security;
|
2015-02-09 17:37:21 +11:00
|
|
|
|
using Microsoft.Owin;
|
2015-02-06 13:47:00 +11:00
|
|
|
|
using Microsoft.Owin.Security;
|
2015-02-09 17:37:21 +11:00
|
|
|
|
using Microsoft.Owin.Security.Cookies;
|
2015-02-06 13:47:00 +11:00
|
|
|
|
using Newtonsoft.Json;
|
2015-02-09 17:37:21 +11:00
|
|
|
|
using Owin;
|
2015-02-06 13:47:00 +11:00
|
|
|
|
using Umbraco.Core.Security;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Security.Identity
|
|
|
|
|
|
{
|
2015-02-09 17:37:21 +11:00
|
|
|
|
|
2015-02-06 13:47:00 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Custom secure format that uses the old FormsAuthentication format
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal class FormsAuthenticationSecureDataFormat : ISecureDataFormat<AuthenticationTicket>
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly int _loginTimeoutMinutes;
|
|
|
|
|
|
|
2015-02-09 17:37:21 +11:00
|
|
|
|
public FormsAuthenticationSecureDataFormat(int loginTimeoutMinutes)
|
2015-02-06 13:47:00 +11:00
|
|
|
|
{
|
|
|
|
|
|
_loginTimeoutMinutes = loginTimeoutMinutes;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string Protect(AuthenticationTicket data)
|
|
|
|
|
|
{
|
2015-02-06 16:13:02 +11:00
|
|
|
|
var backofficeIdentity = (UmbracoBackOfficeIdentity)data.Identity;
|
|
|
|
|
|
var userDataString = JsonConvert.SerializeObject(backofficeIdentity.UserData);
|
2015-06-18 19:16:49 +02:00
|
|
|
|
|
2015-02-06 13:47:00 +11:00
|
|
|
|
var ticket = new FormsAuthenticationTicket(
|
|
|
|
|
|
5,
|
|
|
|
|
|
data.Identity.Name,
|
2015-06-18 19:16:49 +02:00
|
|
|
|
data.Properties.IssuedUtc.HasValue
|
|
|
|
|
|
? data.Properties.IssuedUtc.Value.LocalDateTime
|
|
|
|
|
|
: DateTime.Now,
|
|
|
|
|
|
data.Properties.ExpiresUtc.HasValue
|
|
|
|
|
|
? data.Properties.ExpiresUtc.Value.LocalDateTime
|
|
|
|
|
|
: DateTime.Now.AddMinutes(_loginTimeoutMinutes),
|
2015-02-06 13:47:00 +11:00
|
|
|
|
data.Properties.IsPersistent,
|
2015-02-06 16:13:02 +11:00
|
|
|
|
userDataString,
|
2015-02-09 17:37:21 +11:00
|
|
|
|
"/"
|
2015-02-06 13:47:00 +11:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return FormsAuthentication.Encrypt(ticket);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-02-09 17:37:21 +11:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Unprotects the cookie
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="protectedText"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2015-02-06 13:47:00 +11:00
|
|
|
|
public AuthenticationTicket Unprotect(string protectedText)
|
|
|
|
|
|
{
|
|
|
|
|
|
FormsAuthenticationTicket decrypt;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
decrypt = FormsAuthentication.Decrypt(protectedText);
|
|
|
|
|
|
if (decrypt == null) return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var identity = new UmbracoBackOfficeIdentity(decrypt);
|
|
|
|
|
|
|
2015-02-06 16:13:02 +11:00
|
|
|
|
var ticket = new AuthenticationTicket(identity, new AuthenticationProperties
|
2015-02-06 13:47:00 +11:00
|
|
|
|
{
|
|
|
|
|
|
ExpiresUtc = decrypt.Expiration.ToUniversalTime(),
|
|
|
|
|
|
IssuedUtc = decrypt.IssueDate.ToUniversalTime(),
|
2015-06-18 19:16:49 +02:00
|
|
|
|
IsPersistent = decrypt.IsPersistent,
|
|
|
|
|
|
AllowRefresh = true
|
2015-02-06 13:47:00 +11:00
|
|
|
|
});
|
2015-02-06 16:13:02 +11:00
|
|
|
|
|
|
|
|
|
|
return ticket;
|
2015-02-06 13:47:00 +11:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|