Created
November 1, 2013 15:48
-
-
Save tugberkugurlu/7267386 to your computer and use it in GitHub Desktop.
OwinController for ASP.NET MVC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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