Skip to content

Instantly share code, notes, and snippets.

@sitefinitysteve
Created February 23, 2016 16:37
Show Gist options
  • Save sitefinitysteve/62ab761256a64a84d8a6 to your computer and use it in GitHub Desktop.
Save sitefinitysteve/62ab761256a64a84d8a6 to your computer and use it in GitHub Desktop.
ServiceStack Sitefinity\Auth0 Check
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServiceStack.Web;
using System.Diagnostics;
using Medportal.Sitefinity.Controls;
using JWT;
namespace ServiceStack.ServiceInterface
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class SitefinityJWT : RequestFilterAttribute
{
public SitefinityJWT(ApplyTo applyTo)
: base(applyTo)
{
this.Priority = (int) RequestFilterPriority.Authenticate;
}
public SitefinityJWT()
: this(ApplyTo.All) { }
public override void Execute(IRequest req, IResponse res, object requestDto)
{
if (Util.IsAnonymous)
{
if (req.Headers["Auth0Token"] != null)
{
//Check token
var tokenValue = req.Headers["Auth0Token"];
var user = Medportal.Services.Auth.JsonWebToken.ParseToken(tokenValue, Util.MpConfig.Auth0.ClientSecret, true, true);
if (user.IsExpired)
{
this.BlockExpiredToken();
}
else {
//Do something with the user
}
}
else {
this.BlockAnonymous();
}
}
else {
//User is logged into SF
}
}
private void BlockAnonymous()
{
throw new UnauthorizedAccessException("You must be logged in to access this service");
}
private void BlockExpiredToken()
{
throw new UnauthorizedAccessException("Token Expired");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment