Skip to content

Instantly share code, notes, and snippets.

@wescleymatos
Created October 9, 2018 20:01
Show Gist options
  • Save wescleymatos/69fc467a29c0cbd6b05088d8bdafcf7d to your computer and use it in GitHub Desktop.
Save wescleymatos/69fc467a29c0cbd6b05088d8bdafcf7d to your computer and use it in GitHub Desktop.
Arquivo de startup web api 2
namespace Interface.Api
{
public class Startup
{
private static readonly string _urlBase = ConfigurationManager.AppSettings["UrlBase"];
private static readonly string _allowInsecureHttp = ConfigurationManager.AppSettings["AllowInsecureHttp"];
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration
{
DependencyResolver = new UnityDependencyResolver(UnityConfig.RegisterComponents())
};
ConfigureWebApi(config);
ConfigureContracts(config);
ConfigureOAuth(app);
ConfigureCors(app);
app.UseWebApi(config);
}
private static void ConfigureWebApi(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
private static void ConfigureOAuth(IAppBuilder app)
{
var optionsConfigurationsToken = new OAuthAuthorizationServerOptions
{
AllowInsecureHttp = Convert.ToBoolean(_allowInsecureHttp),
TokenEndpointPath = new PathString("/api/v1/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromHours(8),
// Para gerar Bearer token
Provider = new ProviderTokensAccess(),
// Para gerar token JWT
// Provider = new CustomOAuthProviderJwt(),
// AccessTokenFormat = new CustomJwtFormat(_urlBase)
};
app.UseOAuthAuthorizationServer(optionsConfigurationsToken);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
private static void ConfigureContracts(HttpConfiguration config)
{
//config.Formatters.OfType<JsonMediaTypeFormatter>().First();
//config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false;
config.Formatters.JsonFormatter.SerializerSettings.Re‌​ferenceLoopHandling = ReferenceLoopHandling.Ignore;
}
private static void ConfigureCors(IAppBuilder app)
{
var policy = new CorsPolicy
{
AllowAnyHeader = true,
AllowAnyMethod = true,
AllowAnyOrigin = true
};
//politica.Origins.Add("http://localhost:40874");
//politica.Origins.Add("http://localhost:40880");
//politica.Methods.Add("GET");
//politica.Methods.Add("POST");
var corsOptions = new CorsOptions
{
PolicyProvider = new CorsPolicyProvider
{
PolicyResolver = context => Task.FromResult(policy)
}
};
app.UseCors(corsOptions);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment