protected void Application_Start() { AutofacConfigurator.RegisterComponents(); AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); }
public void Configuration(IAppBuilder app) { HttpConfiguration config = new HttpConfiguration(); SwaggerConfig.Register(config); ConfigurationOAuth(app); WebApiConfig.Register(config); AutofacConfigurator.ConfigureContainer(config, Assembly.GetExecutingAssembly()); app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); app.UseWebApi(config); MappingInitializer.Init(); }
protected void Application_Start() { //Autofac initialisation var config = GlobalConfiguration.Configuration; IContainer container = new AutofacConfigurator().Configure(); config.DependencyResolver = new AutofacWebApiDependencyResolver(container); GlobalConfiguration.Configure(WebApiConfig.Register); RegisterHandlers(); }
// This method gets called by the runtime. Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddDbContext <TextDbContext>(options => options.UseSqlServer(config.GetConnectionString("SampleDatabase"))); services.AddLocalization(o => { // We will put our translations in a folder called Resources o.ResourcesPath = "Resources"; }); var mapperConfig = new MapperConfiguration(c => { c.AddProfile(new Maper()); }); var mapper = mapperConfig.CreateMapper(); services.AddSingleton(mapper); services.AddCors(options => { options.AddPolicy(MyAllowAnyOrigins, builder => { builder.AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod(); }); }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Version = "v1", Title = "WordApp API", Description = "WordApp Web API", TermsOfService = "None", Contact = new Swashbuckle.AspNetCore.Swagger.Contact { Name = "Milos Mijatovic", Email = "*****@*****.**" } }); }); services.AddSingleton <ISharedLocalizer, SharedResource>(); return(AutofacConfigurator.ConfigureAutofacDI(services)); }
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); var userFactory = AutofacConfigurator.Resolve <IUserFactory>(); try { var user = await userFactory.GetUserAsync(context.UserName, context.Password); var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.UserId, ClaimValueTypes.String)); identity.AddClaim(new Claim("sub", context.UserName)); identity.AddClaim(new Claim("role", "user")); context.Validated(identity); } catch (UserNotFoundException) { context.SetError("invalid credentials", "User not found"); return; } }
// ConfigureServices is where you register dependencies. // This method gets called by the runtime. // Use this method to add services to the container. public IServiceProvider ConfigureServices(IServiceCollection services) { // Add services to the collection. services .AddMvc() .SetCompatibilityVersion(CompatibilityVersion.Version_2_1) .AddXmlDataContractSerializerFormatters() .AddMvcOptions(options => { options.FormatterMappings.SetMediaTypeMappingForFormat( Format.XmlFormat.Xml, new MediaTypeHeaderValue(Format.XmlFormat.ApplicationXml)); }); services.AddMemoryCache(); _jwtSetting = Configuration.GetSection(nameof(JwtSetting)) .Get <JwtSetting>(); _dbmsSetting = Configuration.GetSection(nameof(DBMSSetting)) .Get <DBMSSetting>(); _connectionString = Configuration.GetSection(nameof(ConnectionString)) .Get <ConnectionString>(); _sqlServerConnectionString = Configuration.GetSection(nameof(SQLServerConnectionString)) .Get <SQLServerConnectionString>(); _postgreSQLConnectionString = Configuration.GetSection(nameof(PostgreSQLConnectionString)) .Get <PostgreSQLConnectionString>(); _cacheSetting = Configuration.GetSection(nameof(CacheSetting)) .Get <CacheSetting>(); _appSetting = Configuration.GetSection(nameof(AppSetting)) .Get <AppSetting>(); services .Configure <JwtSetting>( Configuration.GetSection(nameof(JwtSetting))) .Configure <DBMSSetting>( Configuration.GetSection(nameof(DBMSSetting))) .Configure <ConnectionString>( Configuration.GetSection(nameof(ConnectionString))) .Configure <SQLServerConnectionString>( Configuration.GetSection(nameof(SQLServerConnectionString))) .Configure <PostgreSQLConnectionString>( Configuration.GetSection(nameof(PostgreSQLConnectionString))) .Configure <CacheSetting>( Configuration.GetSection(nameof(CacheSetting))) .Configure <AppSetting>( Configuration.GetSection(nameof(AppSetting))); // Set authentication scheme for JWT and set JWT provider configuration. services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.RequireHttpsMetadata = false; options.SaveToken = true; options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = false, ValidateIssuerSigningKey = true, ValidateAudience = false, ValidateLifetime = true, IssuerSigningKey = new SymmetricSecurityKey( Encoding.ASCII.GetBytes(_jwtSetting.Key)) }; }); // Register the Swagger generator, defining 1 or more Swagger documents. services.AddSwaggerGen(swagger => { swagger.SwaggerDoc("v1", new Info { Title = Parameters.Authorization, Version = "v1" }); swagger.AddSecurityDefinition(Parameters.Bearer, new ApiKeyScheme { In = Parameters.Header.FirstCharToLower(), Description = Resource.SwaggerSecurityDefinition, Name = Parameters.Authorization, Type = Parameters.ApiKey.FirstCharToLower() }); swagger.AddSecurityRequirement(new Dictionary <string, IEnumerable <string> > { { Parameters.Bearer, new string[] { } } }); }); // Create the IServiceProvider based on the container. return(AutofacConfigurator.GetAutofacServiceProvider( services, ApplicationContainer, DBMSSetting.GetDBMS(_dbmsSetting), DBMSSetting.GetConnectionString( _dbmsSetting, _connectionString, _sqlServerConnectionString, _postgreSQLConnectionString) )); }