Last active
October 19, 2017 06:11
-
-
Save HamidMosalla/29bbe4b287f78463c0b0beb211dddb42 to your computer and use it in GitHub Desktop.
This file contains 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 Startup | |
{ | |
public Startup(IConfiguration configuration, IHostingEnvironment env) | |
{ | |
var builder = new ConfigurationBuilder() | |
.SetBasePath(env.ContentRootPath) | |
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); | |
Configuration = builder.Build(); | |
} | |
public IConfiguration Configuration { get; } | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddDbContext<ApplicationDbContext>(options => | |
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); | |
services.AddIdentity<ApplicationUser, IdentityRole>(option => | |
{ | |
option.Password.RequireDigit = false; | |
option.Password.RequiredLength = 3; | |
option.Password.RequiredUniqueChars = 0; | |
option.Password.RequireLowercase = false; | |
option.Password.RequireNonAlphanumeric = false; | |
option.Password.RequireUppercase = false; | |
}).AddEntityFrameworkStores<ApplicationDbContext>() | |
.AddDefaultTokenProviders(); | |
services.AddAuthentication( /*o => | |
{ | |
o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; | |
o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; | |
o.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme; | |
}*/ /*Uncomment this if you don't want to use JWT for all of your api*/) | |
.AddJwtBearer(cfg => | |
{ | |
cfg.RequireHttpsMetadata = false; | |
cfg.SaveToken = true; | |
cfg.TokenValidationParameters = new TokenValidationParameters() | |
{ | |
ValidIssuer = Configuration["TokenOptions:Issuer"], | |
ValidAudience = Configuration["TokenOptions:Issuer"], | |
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["TokenOptions:Key"])), | |
}; | |
}); | |
// Add application services. | |
services.AddTransient<IEmailSender, EmailSender>(); | |
services.AddMvc(); | |
services.AddAuthorization(options => options.AddPolicy("Trusted", policy => policy.RequireClaim("Employee", "Mosalla"))); | |
services.AddOptions(); | |
services.Configure<TokenOptions>(Configuration.GetSection("TokenOptions")); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | |
{ | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
app.UseBrowserLink(); | |
app.UseDatabaseErrorPage(); | |
} | |
else | |
{ | |
app.UseExceptionHandler("/Home/Error"); | |
} | |
app.UseStaticFiles(); | |
app.UseAuthentication(); | |
app.UseMvc(routes => | |
{ | |
routes.MapRoute( | |
name: "default", | |
template: "{controller=Home}/{action=Index}/{id?}"); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment