Created
May 3, 2020 21:37
-
-
Save manoj-choudhari-git/20e7e2f4868214c4db401668034175e4 to your computer and use it in GitHub Desktop.
Startup.cs for Web App calling Web API Scenario
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) | |
{ | |
Configuration = configuration; | |
} | |
public IConfiguration Configuration { get; } | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.Configure<CookiePolicyOptions>(options => | |
{ | |
// This lambda determines whether user consent for non-essential cookies is needed for a given request. | |
options.CheckConsentNeeded = context => true; | |
options.MinimumSameSitePolicy = SameSiteMode.Unspecified; | |
// Handling SameSite cookie according to https://docs.microsoft.com/en-us/aspnet/core/security/samesite?view=aspnetcore-3.1 | |
options.HandleSameSiteCookieCompatibility(); | |
}); | |
services.AddSignIn(Configuration, "AzureAd"); | |
// Below code is required to call Web API from Web App | |
services.AddWebAppCallsProtectedWebApi(Configuration, | |
initialScopes: new string[] { "user.read", "api://5e971e5c-a661-4d82-ba97-935480492129/access_as_user" }) | |
.AddInMemoryTokenCaches(); | |
// Then, choose Token Cache implementation, InMemory or RedisCache or SQL Table etc. | |
// For instance, the distributed in-memory cache (not cleared when you stop the app): | |
services.AddDistributedMemoryCache(); | |
services.AddRazorPages().AddMvcOptions(options => | |
{ | |
var policy = new AuthorizationPolicyBuilder() | |
.RequireAuthenticatedUser() | |
.Build(); | |
options.Filters.Add(new AuthorizeFilter(policy)); | |
}).AddMicrosoftIdentityUI(); | |
services.AddControllersWithViews(); | |
} | |
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | |
{ | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} | |
else | |
{ | |
app.UseExceptionHandler("/Home/Error"); | |
app.UseHsts(); | |
} | |
app.UseHttpsRedirection(); | |
app.UseStaticFiles(); | |
app.UseCookiePolicy(); | |
app.UseRouting(); | |
app.UseAuthentication(); | |
app.UseAuthorization(); | |
app.UseEndpoints(endpoints => | |
{ | |
endpoints.MapControllerRoute( | |
name: "default", | |
pattern: "{controller=Home}/{action=Index}/{id?}"); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment