using System.Collections.Generic; using System.IdentityModel.Tokens.Jwt; using System.Net.Http; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Protocols.OpenIdConnect; using Microsoft.IdentityModel.Tokens; namespace Mvc.Client { public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddAuthentication(options => { options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; }); services.AddMvc(); services.AddSingleton(); } public void Configure(IApplicationBuilder app) { app.UseDeveloperExceptionPage(); app.UseStaticFiles(); // Insert a new cookies middleware in the pipeline to store the user // identity after he has been redirected from the identity provider. app.UseCookieAuthentication(new CookieAuthenticationOptions { AutomaticAuthenticate = true, AutomaticChallenge = true, LoginPath = new PathString("/signin") }); app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions { // Note: these settings must match the application details // inserted in the database at the server level. ClientId = "mvc", ClientSecret = "901564A5-E7FE-42CB-B10D-61EF6A8F3654", PostLogoutRedirectUri = "http://localhost:53507/", RequireHttpsMetadata = false, GetClaimsFromUserInfoEndpoint = true, SaveTokens = true, // Use the authorization code flow. ResponseType = OpenIdConnectResponseType.Code, AuthenticationMethod = OpenIdConnectRedirectBehavior.RedirectGet, // Note: setting the Authority allows the OIDC client middleware to automatically // retrieve the identity provider's configuration and spare you from setting // the different endpoints URIs or the token validation parameters explicitly. Authority = "http://localhost:54540/", Scope = { "email", "roles", "offline_access" }, SecurityTokenValidator = new JwtSecurityTokenHandler { // Disable the built-in JWT claims mapping feature. InboundClaimTypeMap = new Dictionary() }, TokenValidationParameters = new TokenValidationParameters { NameClaimType = "name", RoleClaimType = "role" } }); app.UseMvc(); } } }