Skip to content

Instantly share code, notes, and snippets.

@abhishekluv
Created February 10, 2020 12:04
Show Gist options
  • Save abhishekluv/f9d5014406304b4afd2284e3d9e78bdc to your computer and use it in GitHub Desktop.
Save abhishekluv/f9d5014406304b4afd2284e3d9e78bdc to your computer and use it in GitHub Desktop.
ASP.NET Core Source Code Demo 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ASPNETCoreDay1213.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;
using Microsoft.IdentityModel.Tokens;
using System.Text;
namespace ASPNETCoreDay1213
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddIdentity<CustomUser, IdentityRole>(cfg =>
{
cfg.User.RequireUniqueEmail = true;
}).AddEntityFrameworkStores<DatabaseContext>();
//we want to use jwt token for this application
services.AddAuthentication()
.AddJwtBearer(cfg =>
{
cfg.TokenValidationParameters = new TokenValidationParameters()
{
ValidIssuer = Configuration["Tokens:Issuer"],
ValidAudience = Configuration["Tokens:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Tokens:Key"]))
};
});
services.AddDbContextPool<DatabaseContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("EFCoreWithAPI"));
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment