Skip to content

Instantly share code, notes, and snippets.

@moo2u2
Last active February 24, 2021 01:39
Show Gist options
  • Save moo2u2/309e71d8b8627b6aea3681c894d792c7 to your computer and use it in GitHub Desktop.
Save moo2u2/309e71d8b8627b6aea3681c894d792c7 to your computer and use it in GitHub Desktop.
Version of Sitecore's CreateTicket processor for sites using CallbackAuthority setting
using Microsoft.Owin;
using Sitecore.Abstractions;
using Sitecore.Configuration.KnownSettings;
using Sitecore.Diagnostics;
using Sitecore.Owin.Authentication.IdentityServer.Extensions;
using Sitecore.Owin.Authentication.Pipelines.CookieAuthentication.SignedIn;
using Sitecore.Owin.Authentication.Security;
using System;
namespace Identity.Pipelines.CookieAuthentication.SignedIn
{
public class CustomCreateTicket : CreateTicket
{
public CustomCreateTicket(BaseTicketManager ticketManager, BaseSettings settings) : base(ticketManager, settings)
{
}
public override void Process(SignedInArgs args)
{
Assert.ArgumentNotNull(args, nameof(args));
if (args.AuthenticationSource != AuthenticationSource.Default || !args.Site.IsBackend && !string.Equals(args.Site.Name, "admin", StringComparison.Ordinal))
return;
string startUrl = args.StartUrl;
string callbackAuthority = Settings.IdentityServer().CallbackAuthority;
if (!string.IsNullOrEmpty(callbackAuthority) && Uri.TryCreate(callbackAuthority, UriKind.Absolute, out Uri callbackUrl))
{
Log.Info($"Replacing ticket URL currently: {startUrl} proxy: {callbackUrl} request: {args.Context.Request.Uri}", this);
startUrl = startUrl.Replace(callbackUrl.GetLeftPart(UriPartial.Authority), args.Context.Request.Uri.GetLeftPart(UriPartial.Authority));
}
string ticket = TicketManager.CreateTicket(args.User.UserName, startUrl, true);
if (string.IsNullOrEmpty(ticket))
return;
CookieOptions options = new CookieOptions()
{
HttpOnly = true,
Expires = new DateTime?(DateTime.UtcNow.Add(Settings.Authentication().ClientPersistentLoginDuration))
};
args.Context.OwinContext.Response.Cookies.Append("sitecore_userticket", ticket, options);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment