Created
January 7, 2014 12:55
-
-
Save ajtowf/8298916 to your computer and use it in GitHub Desktop.
WebApi2 config with OAuth setup
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 class MvcApplication : HttpApplication | |
{ | |
protected void Application_Start() | |
{ | |
AreaRegistration.RegisterAllAreas(); | |
GlobalConfiguration.Configure(WebApiConfig.Register); | |
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); | |
RouteConfig.RegisterRoutes(RouteTable.Routes); | |
BundleConfig.RegisterBundles(BundleTable.Bundles); | |
} | |
} |
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 partial class Startup | |
{ | |
static Startup() | |
{ | |
PublicClientId = "self"; | |
UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>()); | |
OAuthOptions = new OAuthAuthorizationServerOptions | |
{ | |
TokenEndpointPath = new PathString("/Token"), | |
Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory), | |
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"), | |
AccessTokenExpireTimeSpan = TimeSpan.FromDays(14), | |
AllowInsecureHttp = true | |
}; | |
} | |
public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; } | |
public static Func<UserManager<IdentityUser>> UserManagerFactory { get; set; } | |
public static string PublicClientId { get; private set; } | |
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864 | |
public void ConfigureAuth(IAppBuilder app) | |
{ | |
// Enable the application to use a cookie to store information for the signed in user | |
// and to use a cookie to temporarily store information about a user logging in with a third party login provider | |
app.UseCookieAuthentication(new CookieAuthenticationOptions()); | |
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); | |
// Enable the application to use bearer tokens to authenticate users | |
app.UseOAuthBearerTokens(OAuthOptions); | |
// Uncomment the following lines to enable logging in with third party login providers | |
//app.UseMicrosoftAccountAuthentication( | |
// clientId: "", | |
// clientSecret: ""); | |
//app.UseTwitterAuthentication( | |
// consumerKey: "", | |
// consumerSecret: ""); | |
//app.UseFacebookAuthentication( | |
// appId: "", | |
// appSecret: ""); | |
//app.UseGoogleAuthentication(); | |
} | |
} |
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 partial class Startup | |
{ | |
public void Configuration(IAppBuilder app) | |
{ | |
ConfigureAuth(app); | |
} | |
} |
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 static class WebApiConfig | |
{ | |
public static void Register(HttpConfiguration config) | |
{ | |
// Web API configuration and services | |
// Configure Web API to use only bearer token authentication. | |
config.SuppressDefaultHostAuthentication(); | |
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); | |
// Use camel case for JSON data. | |
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); | |
// Web API routes | |
config.MapHttpAttributeRoutes(); | |
config.Routes.MapHttpRoute( | |
name: "DefaultApi", | |
routeTemplate: "api/{controller}/{id}", | |
defaults: new { id = RouteParameter.Optional } | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment