Skip to content

Instantly share code, notes, and snippets.

@tugberkugurlu
Created November 1, 2013 15:48
Show Gist options
  • Save tugberkugurlu/7267386 to your computer and use it in GitHub Desktop.
Save tugberkugurlu/7267386 to your computer and use it in GitHub Desktop.
OwinController for ASP.NET MVC
public abstract class OwinController : Controller
{
private const string OwinEnvironmentKey = "owin.Environment";
protected OwinRequest OwinRequest { get { return GetOwinRequest(HttpContext); } }
protected OwinResponse OwinResponse { get { return GetOwinResponse(HttpContext); } }
protected virtual void SignIn(ClaimsPrincipal principal)
{
}
protected virtual void SignIn(string authenticationType, IEnumerable<Claim> claims, string nameClaimType, string roleClaimType, bool isPersistent)
{
if (authenticationType == null)
{
throw new ArgumentNullException("authenticationType");
}
if (claims == null)
{
throw new ArgumentNullException("claims");
}
if (nameClaimType == null)
{
throw new ArgumentNullException("nameClaimType");
}
if (roleClaimType == null)
{
throw new ArgumentNullException("roleClaimType");
}
SignIn(new ClaimsPrincipal(new ClaimsIdentity(claims, authenticationType, nameClaimType, roleClaimType)));
}
// private helpers
private static IDictionary<string, object> GetOwinEnvironment(HttpContextBase httpContext)
{
return (IDictionary<string, object>)httpContext.Items[(object)OwinEnvironmentKey];
}
private static OwinRequest GetOwinRequest(HttpContextBase httpContext)
{
IDictionary<string, object> owinEnvironment = GetOwinEnvironment(httpContext);
if (owinEnvironment == null)
{
throw new InvalidOperationException("Could not found the OWIN environment");
}
return new OwinRequest(owinEnvironment);
}
private static OwinResponse GetOwinResponse(HttpContextBase httpContext)
{
IDictionary<string, object> owinEnvironment = GetOwinEnvironment(httpContext);
if (owinEnvironment == null)
{
throw new InvalidOperationException("Could not found the OWIN environment");
}
return new OwinResponse(owinEnvironment);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment