Created
April 22, 2023 10:00
-
-
Save thivi/d2c198937c8df0fb99a95d24f65c807f 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
using Microsoft.AspNetCore.Authentication.Cookies; | |
using Microsoft.AspNetCore.Authentication.OpenIdConnect; | |
using Microsoft.IdentityModel.Protocols.OpenIdConnect; | |
using Microsoft.Extensions.Configuration; | |
IConfiguration Configuration = new ConfigurationBuilder() | |
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory) | |
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) | |
.Build(); | |
var builder = WebApplication.CreateBuilder(args); | |
// Add services to the container. | |
builder.Services.AddControllersWithViews(); | |
// Connect to Asgardeo for authentication. | |
builder.Services.AddAuthentication(options => { | |
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; | |
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; | |
}) | |
.AddCookie() | |
.AddOpenIdConnect(options => | |
{ | |
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; | |
options.ClientId = Configuration["Asgardeo:ClientID"]; | |
options.ClientSecret = Configuration["Asgardeo:ClientSecret"]; | |
options.Authority = $"https://api.asgardeo.io/t/{Configuration["Asgardeo:Tenant"]}/oauth2/token"; | |
options.ResponseType = OpenIdConnectResponseType.Code; | |
options.ResponseMode = OpenIdConnectResponseMode.Query; | |
options.SaveTokens = true; | |
options.Scope.Add("profile"); | |
options.Scope.Add("openid"); | |
}); | |
var app = builder.Build(); | |
// Configure the HTTP request pipeline. | |
if (!app.Environment.IsDevelopment()) | |
{ | |
app.UseExceptionHandler("/Home/Error"); | |
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. | |
app.UseHsts(); | |
} | |
app.UseHttpsRedirection(); | |
app.UseStaticFiles(); | |
app.UseRouting(); | |
app.UseAuthentication(); | |
app.UseAuthorization(); | |
app.MapControllerRoute( | |
name: "default", | |
pattern: "{controller=Home}/{action=Index}/{id?}"); | |
app.Run(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment