Skip to content

Instantly share code, notes, and snippets.

@theit8514
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save theit8514/61bbdf9d043de000f752 to your computer and use it in GitHub Desktop.

Select an option

Save theit8514/61bbdf9d043de000f752 to your computer and use it in GitHub Desktop.
Tenant redirect for IS3
public class AcrHelper
{
public static string GetAcrValue(IEnumerable<string> acrValues, string acrKey)
{
acrKey = string.Format("{0}:", acrKey);
var acrKeyValue = acrValues.SingleOrDefault(x => x.StartsWith(acrKey));
if (acrKeyValue == null) return null;
var acrValue = acrKeyValue.Substring(acrKey.Length);
return acrValue;
}
public static IEnumerable<string> FromSpaceSeparatedString(string input)
{
input = input.Trim();
return input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
}
public class Startup
{
public const string AppRoot = "/core";
public void Configuration(IAppBuilder app)
{
app.Map(AppRoot, delegate(IAppBuilder coreApp)
{
coreApp.Use(InterceptNoTenant);
coreApp.UseIdentityServer(...);
});
}
private static async Task InterceptNoTenant(IOwinContext env, Func<Task> next)
{
var pathMatch = new PathString("/connect/authorize");
try
{
var path = env.Request.Path;
PathString remainingPath;
if (path.StartsWithSegments(pathMatch, out remainingPath))
{
// If we have a query string
if (env.Request.QueryString.HasValue)
{
// Parse it and find the acr_values.
var queryString = HttpUtility.ParseQueryString(env.Request.QueryString.Value);
string value;
if (!String.IsNullOrWhiteSpace(queryString.Get("setcookie")))
{
await next();
return;
}
if (!String.IsNullOrWhiteSpace((value = queryString.Get("acr_values"))))
{
// Find the tenant in the values.
var acr = AcrHelper.FromSpaceSeparatedString(value);
var tenant = AcrHelper.GetAcrValue(acr, "tenant");
if (!String.IsNullOrWhiteSpace(tenant))
{
// Set the cookie and continue authentication.
TenantHelper.SetCookie(env, tenant);
var url = env.Request.Uri.AddParameter("setcookie", "true");
env.Response.Redirect(url.ToString());
return;
}
}
}
// Check the cookie
if (!TenantHelper.HasCookie(env))
{
// If no cookie, then redirect to tenant select.
var url = env.Request.PathBase.Add(new PathString("/tenant")).Add(new QueryString("returnUrl", env.Request.Uri.ToString()));
env.Response.Redirect(url);
return;
}
// Here, we should have a cookie set
}
}
catch (Exception ex)
{
Logger.Error("Failed to intercept client with no tenant", ex);
}
await next();
}
}
public class TenantHelper
{
public const string CookieA = "CookieA";
public static bool HasCookie(IOwinContext context) {
return !String.IsNullOrWhiteSpace(context.Request.Cookies[CookieA]);
}
public static void SetCookie(IOwinContext context, string cookieValue)
{
context.Response.Cookies.Append(CookieA, cookieValue, new CookieOptions()
{
Expires = DateTime.Today.AddDays(30),
HttpOnly = true,
Secure = true
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment