From 20fcbe85da1a51c585ae46db52124903e5959134 Mon Sep 17 00:00:00 2001 From: Jerrie Pelser Date: Tue, 1 Aug 2017 15:36:18 -0500 Subject: [PATCH] Update quickstarts to 2.0.0-preview2-final --- .../00-Starter-Seed/SampleMvcApp/Program.cs | 18 +- .../SampleMvcApp/Project_Readme.html | 187 ------------------ .../SampleMvcApp/SampleMvcApp.csproj | 41 +--- .../00-Starter-Seed/SampleMvcApp/Startup.cs | 33 +--- .../01-Login/SampleMvcApp/Auth0Settings.cs | 18 -- .../Controllers/AccountController.cs | 14 +- Quickstart/01-Login/SampleMvcApp/Program.cs | 18 +- .../01-Login/SampleMvcApp/Project_Readme.html | 187 ------------------ .../01-Login/SampleMvcApp/SampleMvcApp.csproj | 41 +--- Quickstart/01-Login/SampleMvcApp/Startup.cs | 110 +++++------ .../01-Login/SampleMvcApp/appsettings.json | 3 +- .../Controllers/AccountController.cs | 28 +-- .../02-Login-Custom/SampleMvcApp/Program.cs | 18 +- .../SampleMvcApp/Project_Readme.html | 187 ------------------ .../SampleMvcApp/SampleMvcApp.csproj | 44 +---- .../02-Login-Custom/SampleMvcApp/Startup.cs | 119 +++++------ .../SampleMvcApp/appsettings.json | 3 +- .../SampleMvcApp/wwwroot/css/site.min.css | 2 +- .../SampleMvcApp/Auth0Settings.cs | 18 -- .../Controllers/AccountController.cs | 14 +- .../Controllers/HomeController.cs | 13 +- .../03-Storing-Tokens/SampleMvcApp/Program.cs | 18 +- .../SampleMvcApp/Project_Readme.html | 187 ------------------ .../SampleMvcApp/SampleMvcApp.csproj | 41 +--- .../03-Storing-Tokens/SampleMvcApp/Startup.cs | 114 +++++------ .../SampleMvcApp/appsettings.json | 3 +- .../SampleMvcApp/Auth0Settings.cs | 18 -- .../Controllers/AccountController.cs | 17 +- .../04-User-Profile/SampleMvcApp/Program.cs | 18 +- .../SampleMvcApp/Project_Readme.html | 187 ------------------ .../SampleMvcApp/SampleMvcApp.csproj | 41 +--- .../04-User-Profile/SampleMvcApp/Startup.cs | 120 +++++------ .../SampleMvcApp/appsettings.json | 3 +- .../SampleMvcApp/Auth0Settings.cs | 18 -- .../Controllers/AccountController.cs | 21 +- .../05-Authorization/SampleMvcApp/Program.cs | 18 +- .../SampleMvcApp/Project_Readme.html | 187 ------------------ .../SampleMvcApp/SampleMvcApp.csproj | 41 +--- .../05-Authorization/SampleMvcApp/Startup.cs | 119 +++++------ .../SampleMvcApp/Views/Account/Profile.cshtml | 3 - .../SampleMvcApp/appsettings.json | 3 +- 41 files changed, 390 insertions(+), 1903 deletions(-) delete mode 100644 Quickstart/00-Starter-Seed/SampleMvcApp/Project_Readme.html delete mode 100644 Quickstart/01-Login/SampleMvcApp/Auth0Settings.cs delete mode 100644 Quickstart/01-Login/SampleMvcApp/Project_Readme.html delete mode 100644 Quickstart/02-Login-Custom/SampleMvcApp/Project_Readme.html delete mode 100644 Quickstart/03-Storing-Tokens/SampleMvcApp/Auth0Settings.cs delete mode 100644 Quickstart/03-Storing-Tokens/SampleMvcApp/Project_Readme.html delete mode 100644 Quickstart/04-User-Profile/SampleMvcApp/Auth0Settings.cs delete mode 100644 Quickstart/04-User-Profile/SampleMvcApp/Project_Readme.html delete mode 100644 Quickstart/05-Authorization/SampleMvcApp/Auth0Settings.cs delete mode 100644 Quickstart/05-Authorization/SampleMvcApp/Project_Readme.html diff --git a/Quickstart/00-Starter-Seed/SampleMvcApp/Program.cs b/Quickstart/00-Starter-Seed/SampleMvcApp/Program.cs index 67afacb..b51f20f 100644 --- a/Quickstart/00-Starter-Seed/SampleMvcApp/Program.cs +++ b/Quickstart/00-Starter-Seed/SampleMvcApp/Program.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; namespace SampleMvcApp @@ -11,14 +7,12 @@ public class Program { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() + BuildWebHost(args).Run(); + } + + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) .UseStartup() .Build(); - - host.Run(); - } } } diff --git a/Quickstart/00-Starter-Seed/SampleMvcApp/Project_Readme.html b/Quickstart/00-Starter-Seed/SampleMvcApp/Project_Readme.html deleted file mode 100644 index 1a0f5b5..0000000 --- a/Quickstart/00-Starter-Seed/SampleMvcApp/Project_Readme.html +++ /dev/null @@ -1,187 +0,0 @@ - - - - - Welcome to ASP.NET Core - - - - - - - - - - diff --git a/Quickstart/00-Starter-Seed/SampleMvcApp/SampleMvcApp.csproj b/Quickstart/00-Starter-Seed/SampleMvcApp/SampleMvcApp.csproj index 1d77c93..bb6c231 100644 --- a/Quickstart/00-Starter-Seed/SampleMvcApp/SampleMvcApp.csproj +++ b/Quickstart/00-Starter-Seed/SampleMvcApp/SampleMvcApp.csproj @@ -1,45 +1,18 @@  - - netcoreapp1.1 - true - SampleMvcApp - Exe - SampleMvcApp - 1.1.0 - $(PackageTargetFallback);dotnet5.6;portable-net45+win8 + netcoreapp2.0 + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; - - - PreserveNewest - + + - - - - - - - - - - - - - - - + + - - - - - - - + \ No newline at end of file diff --git a/Quickstart/00-Starter-Seed/SampleMvcApp/Startup.cs b/Quickstart/00-Starter-Seed/SampleMvcApp/Startup.cs index f34a9f3..612a5fe 100644 --- a/Quickstart/00-Starter-Seed/SampleMvcApp/Startup.cs +++ b/Quickstart/00-Starter-Seed/SampleMvcApp/Startup.cs @@ -1,54 +1,31 @@ -using System.Security.Claims; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Authentication.Cookies; -using Microsoft.AspNetCore.Authentication.OpenIdConnect; -using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; namespace SampleMvcApp { public class Startup { - public Startup(IHostingEnvironment env) + public Startup(IConfiguration configuration) { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) - .AddEnvironmentVariables(); - Configuration = builder.Build(); + Configuration = configuration; } - public IConfigurationRoot Configuration { get; } + 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) { - // Add framework services. services.AddMvc(); - - // Add functionality to inject IOptions - services.AddOptions(); - - // Add the Auth0 Settings object so it can be injected - services.Configure(Configuration.GetSection("Auth0")); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IOptions auth0Settings) + public void Configure(IApplicationBuilder app, IHostingEnvironment env) { - loggerFactory.AddConsole(Configuration.GetSection("Logging")); - loggerFactory.AddDebug(); - if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); - app.UseBrowserLink(); } else { diff --git a/Quickstart/01-Login/SampleMvcApp/Auth0Settings.cs b/Quickstart/01-Login/SampleMvcApp/Auth0Settings.cs deleted file mode 100644 index 1370f8e..0000000 --- a/Quickstart/01-Login/SampleMvcApp/Auth0Settings.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace SampleMvcApp -{ - public class Auth0Settings - { - public string Domain { get; set; } - - public string CallbackUrl { get; set; } - - public string ClientId { get; set; } - - public string ClientSecret { get; set; } - } -} diff --git a/Quickstart/01-Login/SampleMvcApp/Controllers/AccountController.cs b/Quickstart/01-Login/SampleMvcApp/Controllers/AccountController.cs index d0766b0..19ddcea 100644 --- a/Quickstart/01-Login/SampleMvcApp/Controllers/AccountController.cs +++ b/Quickstart/01-Login/SampleMvcApp/Controllers/AccountController.cs @@ -1,6 +1,7 @@ -using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http.Authentication; using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks; @@ -8,22 +9,23 @@ namespace SampleMvcApp.Controllers { public class AccountController : Controller { - public IActionResult Login(string returnUrl = "/") + public async Task Login(string returnUrl = "/") { - return new ChallengeResult("Auth0", new AuthenticationProperties() { RedirectUri = returnUrl }); + await HttpContext.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, + new AuthenticationProperties() { RedirectUri = returnUrl }); } [Authorize] public async Task Logout() { - await HttpContext.Authentication.SignOutAsync("Auth0", new AuthenticationProperties + await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties { // Indicate here where Auth0 should redirect the user after a logout. // Note that the resulting absolute Uri must be whitelisted in the // **Allowed Logout URLs** settings for the client. RedirectUri = Url.Action("Index", "Home") }); - await HttpContext.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); + await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); } /// diff --git a/Quickstart/01-Login/SampleMvcApp/Program.cs b/Quickstart/01-Login/SampleMvcApp/Program.cs index 67afacb..b51f20f 100644 --- a/Quickstart/01-Login/SampleMvcApp/Program.cs +++ b/Quickstart/01-Login/SampleMvcApp/Program.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; namespace SampleMvcApp @@ -11,14 +7,12 @@ public class Program { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() + BuildWebHost(args).Run(); + } + + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) .UseStartup() .Build(); - - host.Run(); - } } } diff --git a/Quickstart/01-Login/SampleMvcApp/Project_Readme.html b/Quickstart/01-Login/SampleMvcApp/Project_Readme.html deleted file mode 100644 index 1a0f5b5..0000000 --- a/Quickstart/01-Login/SampleMvcApp/Project_Readme.html +++ /dev/null @@ -1,187 +0,0 @@ - - - - - Welcome to ASP.NET Core - - - - - - - - - - diff --git a/Quickstart/01-Login/SampleMvcApp/SampleMvcApp.csproj b/Quickstart/01-Login/SampleMvcApp/SampleMvcApp.csproj index 1d77c93..bb6c231 100644 --- a/Quickstart/01-Login/SampleMvcApp/SampleMvcApp.csproj +++ b/Quickstart/01-Login/SampleMvcApp/SampleMvcApp.csproj @@ -1,45 +1,18 @@  - - netcoreapp1.1 - true - SampleMvcApp - Exe - SampleMvcApp - 1.1.0 - $(PackageTargetFallback);dotnet5.6;portable-net45+win8 + netcoreapp2.0 + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; - - - PreserveNewest - + + - - - - - - - - - - - - - - - + + - - - - - - - + \ No newline at end of file diff --git a/Quickstart/01-Login/SampleMvcApp/Startup.cs b/Quickstart/01-Login/SampleMvcApp/Startup.cs index bdeeabc..2acfc71 100644 --- a/Quickstart/01-Login/SampleMvcApp/Startup.cs +++ b/Quickstart/01-Login/SampleMvcApp/Startup.cs @@ -15,90 +15,51 @@ namespace SampleMvcApp { public class Startup { - public Startup(IHostingEnvironment env) + public Startup(IConfiguration configuration) { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) - .AddEnvironmentVariables(); - Configuration = builder.Build(); + Configuration = configuration; } - public IConfigurationRoot Configuration { get; } + 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) { // Add authentication services - services.AddAuthentication( - options => options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme); - - // Add framework services. - services.AddMvc(); - - // Add functionality to inject IOptions - services.AddOptions(); - - // Add the Auth0 Settings object so it can be injected - services.Configure(Configuration.GetSection("Auth0")); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IOptions auth0Settings) - { - loggerFactory.AddConsole(Configuration.GetSection("Logging")); - loggerFactory.AddDebug(); - - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - app.UseBrowserLink(); - } - else - { - app.UseExceptionHandler("/Home/Error"); - } - - app.UseStaticFiles(); - - // Add the cookie middleware - app.UseCookieAuthentication(new CookieAuthenticationOptions - { - AutomaticAuthenticate = true, - AutomaticChallenge = true - }); - - // Add the OIDC middleware - var options = new OpenIdConnectOptions("Auth0") - { + services.AddAuthentication(options => { + options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; + options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; + }) + .AddCookieAuthentication() + .AddOpenIdConnectAuthentication(options => { // Set the authority to your Auth0 domain - Authority = $"https://{auth0Settings.Value.Domain}", + options.Authority = $"https://{Configuration["Auth0:Domain"]}"; // Configure the Auth0 Client ID and Client Secret - ClientId = auth0Settings.Value.ClientId, - ClientSecret = auth0Settings.Value.ClientSecret, - - // Do not automatically authenticate and challenge - AutomaticAuthenticate = false, - AutomaticChallenge = false, + options.ClientId = Configuration["Auth0:ClientId"]; + options.ClientSecret = Configuration["Auth0:ClientSecret"]; // Set response type to code - ResponseType = "code", + options.ResponseType = "code"; + + // Configure the scope + options.Scope.Clear(); + options.Scope.Add("openid"); // Set the callback path, so Auth0 will call back to http://localhost:5000/signin-auth0 // Also ensure that you have added the URL as an Allowed Callback URL in your Auth0 dashboard - CallbackPath = new PathString("/signin-auth0"), + options.CallbackPath = new PathString("/signin-auth0"); // Configure the Claims Issuer to be Auth0 - ClaimsIssuer = "Auth0", + options.ClaimsIssuer = "Auth0"; - Events = new OpenIdConnectEvents + options.Events = new OpenIdConnectEvents { // handle the logout redirection OnRedirectToIdentityProviderForSignOut = (context) => { - var logoutUri = $"https://{auth0Settings.Value.Domain}/v2/logout?client_id={auth0Settings.Value.ClientId}"; + var logoutUri = $"https://{Configuration["Auth0:Domain"]}/v2/logout?client_id={Configuration["Auth0:ClientId"]}"; var postLogoutUri = context.Properties.RedirectUri; if (!string.IsNullOrEmpty(postLogoutUri)) @@ -117,11 +78,28 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF return Task.CompletedTask; } - } - }; - options.Scope.Clear(); - options.Scope.Add("openid"); - app.UseOpenIdConnectAuthentication(options); + }; + }); + + // Add framework services. + services.AddMvc(); + } + + // 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(); + } + else + { + app.UseExceptionHandler("/Home/Error"); + } + + app.UseStaticFiles(); + + app.UseAuthentication(); app.UseMvc(routes => { diff --git a/Quickstart/01-Login/SampleMvcApp/appsettings.json b/Quickstart/01-Login/SampleMvcApp/appsettings.json index 4c3e4b4..932c0e5 100644 --- a/Quickstart/01-Login/SampleMvcApp/appsettings.json +++ b/Quickstart/01-Login/SampleMvcApp/appsettings.json @@ -10,7 +10,6 @@ "Auth0": { "Domain": "{DOMAIN}", "ClientId": "{CLIENT_ID}", - "ClientSecret": "{CLIENT_SECRET}", - "CallbackUrl": "http://localhost:60856/signin-auth0" + "ClientSecret": "{CLIENT_SECRET}" } } diff --git a/Quickstart/02-Login-Custom/SampleMvcApp/Controllers/AccountController.cs b/Quickstart/02-Login-Custom/SampleMvcApp/Controllers/AccountController.cs index 47d4eb7..da01a31 100644 --- a/Quickstart/02-Login-Custom/SampleMvcApp/Controllers/AccountController.cs +++ b/Quickstart/02-Login-Custom/SampleMvcApp/Controllers/AccountController.cs @@ -1,8 +1,8 @@ using Auth0.AuthenticationApi; using Auth0.AuthenticationApi.Models; +using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http.Authentication; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using SampleMvcApp.ViewModels; @@ -10,16 +10,18 @@ using System.Security.Claims; using System.Threading.Tasks; using Microsoft.Extensions.Localization.Internal; +using Microsoft.AspNetCore.Authentication.OpenIdConnect; +using Microsoft.Extensions.Configuration; namespace SampleMvcApp.Controllers { public class AccountController : Controller { - private readonly Auth0Settings _auth0Settings; + private readonly IConfiguration _configuration; - public AccountController(IOptions auth0Settings) + public AccountController(IConfiguration configuration) { - _auth0Settings = auth0Settings.Value; + this._configuration = configuration; } [HttpGet] @@ -37,12 +39,12 @@ public async Task Login(LoginViewModel vm, string returnUrl = nul { try { - AuthenticationApiClient client = new AuthenticationApiClient(new Uri($"https://{_auth0Settings.Domain}/")); + AuthenticationApiClient client = new AuthenticationApiClient(new Uri($"https://{_configuration["Auth0:Domain"]}/")); var result = await client.GetTokenAsync(new ResourceOwnerTokenRequest { - ClientId = _auth0Settings.ClientId, - ClientSecret = _auth0Settings.ClientSecret, + ClientId = _configuration["Auth0:ClientId"], + ClientSecret = _configuration["Auth0:ClientSecret"], Scope = "openid profile", Realm = "Username-Password-Authentication", // Specify the correct name of your DB connection Username = vm.EmailAddress, @@ -55,13 +57,13 @@ public async Task Login(LoginViewModel vm, string returnUrl = nul // Create claims principal var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new[] { - new Claim(ClaimTypes.NameIdentifier, user.UserId), + new Claim(ClaimTypes.NameIdentifier, user.UserId), new Claim(ClaimTypes.Name, user.FullName) }, CookieAuthenticationDefaults.AuthenticationScheme)); // Sign user into cookie middleware - await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal); + await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal); return RedirectToLocal(returnUrl); } @@ -75,27 +77,27 @@ public async Task Login(LoginViewModel vm, string returnUrl = nul } [HttpGet] - public IActionResult LoginExternal(string connection, string returnUrl = "/") + public async Task LoginExternal(string connection, string returnUrl = "/") { var properties = new AuthenticationProperties() { RedirectUri = returnUrl }; if (!string.IsNullOrEmpty(connection)) properties.Items.Add("connection", connection); - return new ChallengeResult("Auth0", properties); + await HttpContext.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, properties); } [Authorize] public async Task Logout() { - await HttpContext.Authentication.SignOutAsync("Auth0", new AuthenticationProperties + await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties { // Indicate here where Auth0 should redirect the user after a logout. // Note that the resulting absolute Uri must be whitelisted in the // **Allowed Logout URLs** settings for the client. RedirectUri = Url.Action("Index", "Home") }); - await HttpContext.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); + await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); } /// diff --git a/Quickstart/02-Login-Custom/SampleMvcApp/Program.cs b/Quickstart/02-Login-Custom/SampleMvcApp/Program.cs index 67afacb..b51f20f 100644 --- a/Quickstart/02-Login-Custom/SampleMvcApp/Program.cs +++ b/Quickstart/02-Login-Custom/SampleMvcApp/Program.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; namespace SampleMvcApp @@ -11,14 +7,12 @@ public class Program { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() + BuildWebHost(args).Run(); + } + + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) .UseStartup() .Build(); - - host.Run(); - } } } diff --git a/Quickstart/02-Login-Custom/SampleMvcApp/Project_Readme.html b/Quickstart/02-Login-Custom/SampleMvcApp/Project_Readme.html deleted file mode 100644 index 1a0f5b5..0000000 --- a/Quickstart/02-Login-Custom/SampleMvcApp/Project_Readme.html +++ /dev/null @@ -1,187 +0,0 @@ - - - - - Welcome to ASP.NET Core - - - - - - - - - - diff --git a/Quickstart/02-Login-Custom/SampleMvcApp/SampleMvcApp.csproj b/Quickstart/02-Login-Custom/SampleMvcApp/SampleMvcApp.csproj index 5055230..6949f03 100644 --- a/Quickstart/02-Login-Custom/SampleMvcApp/SampleMvcApp.csproj +++ b/Quickstart/02-Login-Custom/SampleMvcApp/SampleMvcApp.csproj @@ -1,47 +1,19 @@  - - netcoreapp1.1 - true - SampleMvcApp - Exe - SampleMvcApp - 1.1.0 - $(PackageTargetFallback);dotnet5.6;portable-net45+win8 + netcoreapp2.0 + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; - - - PreserveNewest - + + + - - - - - - - - - - - - - - - - - + + - - - - - - - + \ No newline at end of file diff --git a/Quickstart/02-Login-Custom/SampleMvcApp/Startup.cs b/Quickstart/02-Login-Custom/SampleMvcApp/Startup.cs index c08235e..2d127a8 100644 --- a/Quickstart/02-Login-Custom/SampleMvcApp/Startup.cs +++ b/Quickstart/02-Login-Custom/SampleMvcApp/Startup.cs @@ -1,6 +1,4 @@ -using System.Security.Claims; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -10,90 +8,54 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System; +using System.Security.Claims; +using System.Threading.Tasks; namespace SampleMvcApp { public class Startup { - public Startup(IHostingEnvironment env) + public Startup(IConfiguration configuration) { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) - .AddEnvironmentVariables(); - Configuration = builder.Build(); + Configuration = configuration; } - public IConfigurationRoot Configuration { get; } + 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) { // Add authentication services - services.AddAuthentication( - options => options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme); - - // Add framework services. - services.AddMvc(); - - // Add functionality to inject IOptions - services.AddOptions(); - - // Add the Auth0 Settings object so it can be injected - services.Configure(Configuration.GetSection("Auth0")); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IOptions auth0Settings) - { - loggerFactory.AddConsole(Configuration.GetSection("Logging")); - loggerFactory.AddDebug(); - - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - app.UseBrowserLink(); - } - else - { - app.UseExceptionHandler("/Home/Error"); - } - - app.UseStaticFiles(); - - // Add the cookie middleware - app.UseCookieAuthentication(new CookieAuthenticationOptions - { - AutomaticAuthenticate = true, - AutomaticChallenge = true - }); - - // Add the OIDC middleware - var options = new OpenIdConnectOptions("Auth0") - { + services.AddAuthentication(options => { + options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; + options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; + }) + .AddCookieAuthentication() + .AddOpenIdConnectAuthentication(options => { // Set the authority to your Auth0 domain - Authority = $"https://{auth0Settings.Value.Domain}", + options.Authority = $"https://{Configuration["Auth0:Domain"]}"; // Configure the Auth0 Client ID and Client Secret - ClientId = auth0Settings.Value.ClientId, - ClientSecret = auth0Settings.Value.ClientSecret, - - // Do not automatically authenticate and challenge - AutomaticAuthenticate = false, - AutomaticChallenge = false, + options.ClientId = Configuration["Auth0:ClientId"]; + options.ClientSecret = Configuration["Auth0:ClientSecret"]; // Set response type to code - ResponseType = "code", + options.ResponseType = "code"; + + // Configure the scope + options.Scope.Clear(); + options.Scope.Add("openid"); + options.Scope.Add("profile"); // Set the callback path, so Auth0 will call back to http://localhost:5000/signin-auth0 // Also ensure that you have added the URL as an Allowed Callback URL in your Auth0 dashboard - CallbackPath = new PathString("/signin-auth0"), + options.CallbackPath = new PathString("/signin-auth0"); // Configure the Claims Issuer to be Auth0 - ClaimsIssuer = "Auth0", + options.ClaimsIssuer = "Auth0"; - Events = new OpenIdConnectEvents + options.Events = new OpenIdConnectEvents { OnRedirectToIdentityProvider = context => { @@ -103,9 +65,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF return Task.FromResult(0); }, // handle the logout redirection - OnRedirectToIdentityProviderForSignOut = context => + OnRedirectToIdentityProviderForSignOut = (context) => { - var logoutUri = $"https://{auth0Settings.Value.Domain}/v2/logout?client_id={auth0Settings.Value.ClientId}"; + var logoutUri = $"https://{Configuration["Auth0:Domain"]}/v2/logout?client_id={Configuration["Auth0:ClientId"]}"; var postLogoutUri = context.Properties.RedirectUri; if (!string.IsNullOrEmpty(postLogoutUri)) @@ -124,11 +86,28 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF return Task.CompletedTask; } - } - }; - options.Scope.Clear(); - options.Scope.Add("openid"); - app.UseOpenIdConnectAuthentication(options); + }; + }); + + // Add framework services. + services.AddMvc(); + } + + // 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(); + } + else + { + app.UseExceptionHandler("/Home/Error"); + } + + app.UseStaticFiles(); + + app.UseAuthentication(); app.UseMvc(routes => { diff --git a/Quickstart/02-Login-Custom/SampleMvcApp/appsettings.json b/Quickstart/02-Login-Custom/SampleMvcApp/appsettings.json index 4c3e4b4..932c0e5 100644 --- a/Quickstart/02-Login-Custom/SampleMvcApp/appsettings.json +++ b/Quickstart/02-Login-Custom/SampleMvcApp/appsettings.json @@ -10,7 +10,6 @@ "Auth0": { "Domain": "{DOMAIN}", "ClientId": "{CLIENT_ID}", - "ClientSecret": "{CLIENT_SECRET}", - "CallbackUrl": "http://localhost:60856/signin-auth0" + "ClientSecret": "{CLIENT_SECRET}" } } diff --git a/Quickstart/02-Login-Custom/SampleMvcApp/wwwroot/css/site.min.css b/Quickstart/02-Login-Custom/SampleMvcApp/wwwroot/css/site.min.css index 3beb45f..db9ef63 100644 --- a/Quickstart/02-Login-Custom/SampleMvcApp/wwwroot/css/site.min.css +++ b/Quickstart/02-Login-Custom/SampleMvcApp/wwwroot/css/site.min.css @@ -1 +1 @@ -body{padding-top:50px;padding-bottom:20px}.body-content{padding-left:15px;padding-right:15px}input,select,textarea{max-width:280px}.carousel-caption p{font-size:20px;line-height:1.4}.carousel-inner .item img[src$=".svg"]{width:100%}@media screen and (max-width:767px){.carousel-caption{display:none}} \ No newline at end of file +body{padding-top:50px;padding-bottom:20px}.body-content{padding-left:15px;padding-right:15px}.carousel-caption p{font-size:20px;line-height:1.4}.carousel-inner .item img[src$=".svg"]{width:100%}@media screen and (max-width:767px){.carousel-caption{display:none}} \ No newline at end of file diff --git a/Quickstart/03-Storing-Tokens/SampleMvcApp/Auth0Settings.cs b/Quickstart/03-Storing-Tokens/SampleMvcApp/Auth0Settings.cs deleted file mode 100644 index 1370f8e..0000000 --- a/Quickstart/03-Storing-Tokens/SampleMvcApp/Auth0Settings.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace SampleMvcApp -{ - public class Auth0Settings - { - public string Domain { get; set; } - - public string CallbackUrl { get; set; } - - public string ClientId { get; set; } - - public string ClientSecret { get; set; } - } -} diff --git a/Quickstart/03-Storing-Tokens/SampleMvcApp/Controllers/AccountController.cs b/Quickstart/03-Storing-Tokens/SampleMvcApp/Controllers/AccountController.cs index d0766b0..19ddcea 100644 --- a/Quickstart/03-Storing-Tokens/SampleMvcApp/Controllers/AccountController.cs +++ b/Quickstart/03-Storing-Tokens/SampleMvcApp/Controllers/AccountController.cs @@ -1,6 +1,7 @@ -using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http.Authentication; using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks; @@ -8,22 +9,23 @@ namespace SampleMvcApp.Controllers { public class AccountController : Controller { - public IActionResult Login(string returnUrl = "/") + public async Task Login(string returnUrl = "/") { - return new ChallengeResult("Auth0", new AuthenticationProperties() { RedirectUri = returnUrl }); + await HttpContext.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, + new AuthenticationProperties() { RedirectUri = returnUrl }); } [Authorize] public async Task Logout() { - await HttpContext.Authentication.SignOutAsync("Auth0", new AuthenticationProperties + await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties { // Indicate here where Auth0 should redirect the user after a logout. // Note that the resulting absolute Uri must be whitelisted in the // **Allowed Logout URLs** settings for the client. RedirectUri = Url.Action("Index", "Home") }); - await HttpContext.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); + await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); } /// diff --git a/Quickstart/03-Storing-Tokens/SampleMvcApp/Controllers/HomeController.cs b/Quickstart/03-Storing-Tokens/SampleMvcApp/Controllers/HomeController.cs index 860e2ce..94d1f00 100644 --- a/Quickstart/03-Storing-Tokens/SampleMvcApp/Controllers/HomeController.cs +++ b/Quickstart/03-Storing-Tokens/SampleMvcApp/Controllers/HomeController.cs @@ -4,13 +4,24 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Authentication; namespace SampleMvcApp.Controllers { public class HomeController : Controller { - public IActionResult Index() + public async Task Index() { + // If the user is authenticated, then this is how you can get the access_token and id_token + if (User.Identity.IsAuthenticated) + { + string accessToken = await HttpContext.GetTokenAsync("access_token"); + string idToken = await HttpContext.GetTokenAsync("id_token"); + + // Now you can use them. For more info on when and how to use the + // access_token and id_token, see https://auth0.com/docs/tokens + } + return View(); } diff --git a/Quickstart/03-Storing-Tokens/SampleMvcApp/Program.cs b/Quickstart/03-Storing-Tokens/SampleMvcApp/Program.cs index 67afacb..b51f20f 100644 --- a/Quickstart/03-Storing-Tokens/SampleMvcApp/Program.cs +++ b/Quickstart/03-Storing-Tokens/SampleMvcApp/Program.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; namespace SampleMvcApp @@ -11,14 +7,12 @@ public class Program { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() + BuildWebHost(args).Run(); + } + + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) .UseStartup() .Build(); - - host.Run(); - } } } diff --git a/Quickstart/03-Storing-Tokens/SampleMvcApp/Project_Readme.html b/Quickstart/03-Storing-Tokens/SampleMvcApp/Project_Readme.html deleted file mode 100644 index 1a0f5b5..0000000 --- a/Quickstart/03-Storing-Tokens/SampleMvcApp/Project_Readme.html +++ /dev/null @@ -1,187 +0,0 @@ - - - - - Welcome to ASP.NET Core - - - - - - - - - - diff --git a/Quickstart/03-Storing-Tokens/SampleMvcApp/SampleMvcApp.csproj b/Quickstart/03-Storing-Tokens/SampleMvcApp/SampleMvcApp.csproj index 1d77c93..bb6c231 100644 --- a/Quickstart/03-Storing-Tokens/SampleMvcApp/SampleMvcApp.csproj +++ b/Quickstart/03-Storing-Tokens/SampleMvcApp/SampleMvcApp.csproj @@ -1,45 +1,18 @@  - - netcoreapp1.1 - true - SampleMvcApp - Exe - SampleMvcApp - 1.1.0 - $(PackageTargetFallback);dotnet5.6;portable-net45+win8 + netcoreapp2.0 + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; - - - PreserveNewest - + + - - - - - - - - - - - - - - - + + - - - - - - - + \ No newline at end of file diff --git a/Quickstart/03-Storing-Tokens/SampleMvcApp/Startup.cs b/Quickstart/03-Storing-Tokens/SampleMvcApp/Startup.cs index e659c15..84d277a 100644 --- a/Quickstart/03-Storing-Tokens/SampleMvcApp/Startup.cs +++ b/Quickstart/03-Storing-Tokens/SampleMvcApp/Startup.cs @@ -8,7 +8,6 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System; -using System.Linq; using System.Security.Claims; using System.Threading.Tasks; @@ -16,94 +15,54 @@ namespace SampleMvcApp { public class Startup { - public Startup(IHostingEnvironment env) + public Startup(IConfiguration configuration) { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) - .AddEnvironmentVariables(); - Configuration = builder.Build(); + Configuration = configuration; } - public IConfigurationRoot Configuration { get; } + 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) { // Add authentication services - services.AddAuthentication( - options => options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme); - - // Add framework services. - services.AddMvc(); - - // Add functionality to inject IOptions - services.AddOptions(); - - // Add the Auth0 Settings object so it can be injected - services.Configure(Configuration.GetSection("Auth0")); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, - IOptions auth0Settings) - { - loggerFactory.AddConsole(Configuration.GetSection("Logging")); - loggerFactory.AddDebug(); - - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - app.UseBrowserLink(); - } - else - { - app.UseExceptionHandler("/Home/Error"); - } - - app.UseStaticFiles(); - - // Add the cookie middleware - app.UseCookieAuthentication(new CookieAuthenticationOptions - { - AutomaticAuthenticate = true, - AutomaticChallenge = true - }); - - // Add the OIDC middleware - var options = new OpenIdConnectOptions("Auth0") - { + services.AddAuthentication(options => { + options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; + options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; + }) + .AddCookieAuthentication() + .AddOpenIdConnectAuthentication(options => { // Set the authority to your Auth0 domain - Authority = $"https://{auth0Settings.Value.Domain}", + options.Authority = $"https://{Configuration["Auth0:Domain"]}"; // Configure the Auth0 Client ID and Client Secret - ClientId = auth0Settings.Value.ClientId, - ClientSecret = auth0Settings.Value.ClientSecret, - - // Do not automatically authenticate and challenge - AutomaticAuthenticate = false, - AutomaticChallenge = false, + options.ClientId = Configuration["Auth0:ClientId"]; + options.ClientSecret = Configuration["Auth0:ClientSecret"]; // Set response type to code - ResponseType = "code", + options.ResponseType = "code"; + + // Configure the scope + options.Scope.Clear(); + options.Scope.Add("openid"); // Set the callback path, so Auth0 will call back to http://localhost:5000/signin-auth0 // Also ensure that you have added the URL as an Allowed Callback URL in your Auth0 dashboard - CallbackPath = new PathString("/signin-auth0"), + options.CallbackPath = new PathString("/signin-auth0"); // Configure the Claims Issuer to be Auth0 - ClaimsIssuer = "Auth0", + options.ClaimsIssuer = "Auth0"; // Saves tokens to the AuthenticationProperties - SaveTokens = true, + options.SaveTokens = true; - Events = new OpenIdConnectEvents() + options.Events = new OpenIdConnectEvents { // handle the logout redirection OnRedirectToIdentityProviderForSignOut = (context) => { - var logoutUri = $"https://{auth0Settings.Value.Domain}/v2/logout?client_id={auth0Settings.Value.ClientId}"; + var logoutUri = $"https://{Configuration["Auth0:Domain"]}/v2/logout?client_id={Configuration["Auth0:ClientId"]}"; var postLogoutUri = context.Properties.RedirectUri; if (!string.IsNullOrEmpty(postLogoutUri)) @@ -122,11 +81,28 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF return Task.CompletedTask; } - } - }; - options.Scope.Clear(); - options.Scope.Add("openid"); - app.UseOpenIdConnectAuthentication(options); + }; + }); + + // Add framework services. + services.AddMvc(); + } + + // 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(); + } + else + { + app.UseExceptionHandler("/Home/Error"); + } + + app.UseStaticFiles(); + + app.UseAuthentication(); app.UseMvc(routes => { diff --git a/Quickstart/03-Storing-Tokens/SampleMvcApp/appsettings.json b/Quickstart/03-Storing-Tokens/SampleMvcApp/appsettings.json index 4c3e4b4..932c0e5 100644 --- a/Quickstart/03-Storing-Tokens/SampleMvcApp/appsettings.json +++ b/Quickstart/03-Storing-Tokens/SampleMvcApp/appsettings.json @@ -10,7 +10,6 @@ "Auth0": { "Domain": "{DOMAIN}", "ClientId": "{CLIENT_ID}", - "ClientSecret": "{CLIENT_SECRET}", - "CallbackUrl": "http://localhost:60856/signin-auth0" + "ClientSecret": "{CLIENT_SECRET}" } } diff --git a/Quickstart/04-User-Profile/SampleMvcApp/Auth0Settings.cs b/Quickstart/04-User-Profile/SampleMvcApp/Auth0Settings.cs deleted file mode 100644 index 1370f8e..0000000 --- a/Quickstart/04-User-Profile/SampleMvcApp/Auth0Settings.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace SampleMvcApp -{ - public class Auth0Settings - { - public string Domain { get; set; } - - public string CallbackUrl { get; set; } - - public string ClientId { get; set; } - - public string ClientSecret { get; set; } - } -} diff --git a/Quickstart/04-User-Profile/SampleMvcApp/Controllers/AccountController.cs b/Quickstart/04-User-Profile/SampleMvcApp/Controllers/AccountController.cs index f19a676..5d4dcf0 100644 --- a/Quickstart/04-User-Profile/SampleMvcApp/Controllers/AccountController.cs +++ b/Quickstart/04-User-Profile/SampleMvcApp/Controllers/AccountController.cs @@ -1,32 +1,34 @@ -using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http.Authentication; using Microsoft.AspNetCore.Mvc; +using System.Threading.Tasks; using SampleMvcApp.ViewModels; using System.Linq; using System.Security.Claims; -using System.Threading.Tasks; namespace SampleMvcApp.Controllers { public class AccountController : Controller { - public IActionResult Login(string returnUrl = "/") + public async Task Login(string returnUrl = "/") { - return new ChallengeResult("Auth0", new AuthenticationProperties() { RedirectUri = returnUrl }); + await HttpContext.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, + new AuthenticationProperties() { RedirectUri = returnUrl }); } [Authorize] public async Task Logout() { - await HttpContext.Authentication.SignOutAsync("Auth0", new AuthenticationProperties + await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties { // Indicate here where Auth0 should redirect the user after a logout. // Note that the resulting absolute Uri must be whitelisted in the // **Allowed Logout URLs** settings for the client. RedirectUri = Url.Action("Index", "Home") }); - await HttpContext.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); + await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); } [Authorize] @@ -40,6 +42,7 @@ public IActionResult Profile() }); } + /// /// This is just a helper action to enable you to easily see all claims related to a user. It helps when debugging your /// application to see the in claims populated from the Auth0 ID Token diff --git a/Quickstart/04-User-Profile/SampleMvcApp/Program.cs b/Quickstart/04-User-Profile/SampleMvcApp/Program.cs index 67afacb..b51f20f 100644 --- a/Quickstart/04-User-Profile/SampleMvcApp/Program.cs +++ b/Quickstart/04-User-Profile/SampleMvcApp/Program.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; namespace SampleMvcApp @@ -11,14 +7,12 @@ public class Program { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() + BuildWebHost(args).Run(); + } + + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) .UseStartup() .Build(); - - host.Run(); - } } } diff --git a/Quickstart/04-User-Profile/SampleMvcApp/Project_Readme.html b/Quickstart/04-User-Profile/SampleMvcApp/Project_Readme.html deleted file mode 100644 index 1a0f5b5..0000000 --- a/Quickstart/04-User-Profile/SampleMvcApp/Project_Readme.html +++ /dev/null @@ -1,187 +0,0 @@ - - - - - Welcome to ASP.NET Core - - - - - - - - - - diff --git a/Quickstart/04-User-Profile/SampleMvcApp/SampleMvcApp.csproj b/Quickstart/04-User-Profile/SampleMvcApp/SampleMvcApp.csproj index 1d77c93..bb6c231 100644 --- a/Quickstart/04-User-Profile/SampleMvcApp/SampleMvcApp.csproj +++ b/Quickstart/04-User-Profile/SampleMvcApp/SampleMvcApp.csproj @@ -1,45 +1,18 @@  - - netcoreapp1.1 - true - SampleMvcApp - Exe - SampleMvcApp - 1.1.0 - $(PackageTargetFallback);dotnet5.6;portable-net45+win8 + netcoreapp2.0 + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; - - - PreserveNewest - + + - - - - - - - - - - - - - - - + + - - - - - - - + \ No newline at end of file diff --git a/Quickstart/04-User-Profile/SampleMvcApp/Startup.cs b/Quickstart/04-User-Profile/SampleMvcApp/Startup.cs index df8a8d6..5ac0b9d 100644 --- a/Quickstart/04-User-Profile/SampleMvcApp/Startup.cs +++ b/Quickstart/04-User-Profile/SampleMvcApp/Startup.cs @@ -16,96 +16,59 @@ namespace SampleMvcApp { public class Startup { - public Startup(IHostingEnvironment env) + public Startup(IConfiguration configuration) { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) - .AddEnvironmentVariables(); - Configuration = builder.Build(); + Configuration = configuration; } - public IConfigurationRoot Configuration { get; } + 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) { // Add authentication services - services.AddAuthentication( - options => options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme); - - // Add framework services. - services.AddMvc(); - - // Add functionality to inject IOptions - services.AddOptions(); - - // Add the Auth0 Settings object so it can be injected - services.Configure(Configuration.GetSection("Auth0")); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IOptions auth0Settings) - { - loggerFactory.AddConsole(Configuration.GetSection("Logging")); - loggerFactory.AddDebug(); - - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - app.UseBrowserLink(); - } - else - { - app.UseExceptionHandler("/Home/Error"); - } - - app.UseStaticFiles(); - - // Add the cookie middleware - app.UseCookieAuthentication(new CookieAuthenticationOptions - { - AutomaticAuthenticate = true, - AutomaticChallenge = true - }); - - // Add the OIDC middleware - var options = new OpenIdConnectOptions("Auth0") - { + services.AddAuthentication(options => { + options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; + options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; + }) + .AddCookieAuthentication() + .AddOpenIdConnectAuthentication(options => { // Set the authority to your Auth0 domain - Authority = $"https://{auth0Settings.Value.Domain}", + options.Authority = $"https://{Configuration["Auth0:Domain"]}"; // Configure the Auth0 Client ID and Client Secret - ClientId = auth0Settings.Value.ClientId, - ClientSecret = auth0Settings.Value.ClientSecret, - - // Do not automatically authenticate and challenge - AutomaticAuthenticate = false, - AutomaticChallenge = false, + options.ClientId = Configuration["Auth0:ClientId"]; + options.ClientSecret = Configuration["Auth0:ClientSecret"]; // Set response type to code - ResponseType = "code", + options.ResponseType = "code"; + + // Configure the scope + options.Scope.Clear(); + options.Scope.Add("openid"); + options.Scope.Add("profile"); + options.Scope.Add("email"); // Set the callback path, so Auth0 will call back to http://localhost:5000/signin-auth0 // Also ensure that you have added the URL as an Allowed Callback URL in your Auth0 dashboard - CallbackPath = new PathString("/signin-auth0"), + options.CallbackPath = new PathString("/signin-auth0"); // Configure the Claims Issuer to be Auth0 - ClaimsIssuer = "Auth0", + options.ClaimsIssuer = "Auth0"; // Set the correct name claim type - TokenValidationParameters = new TokenValidationParameters + options.TokenValidationParameters = new TokenValidationParameters { NameClaimType = "name" - }, + }; - Events = new OpenIdConnectEvents + options.Events = new OpenIdConnectEvents { // handle the logout redirection OnRedirectToIdentityProviderForSignOut = (context) => { - var logoutUri = $"https://{auth0Settings.Value.Domain}/v2/logout?client_id={auth0Settings.Value.ClientId}"; + var logoutUri = $"https://{Configuration["Auth0:Domain"]}/v2/logout?client_id={Configuration["Auth0:ClientId"]}"; var postLogoutUri = context.Properties.RedirectUri; if (!string.IsNullOrEmpty(postLogoutUri)) @@ -124,13 +87,28 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF return Task.CompletedTask; } - } - }; - options.Scope.Clear(); - options.Scope.Add("openid"); - options.Scope.Add("profile"); - options.Scope.Add("email"); - app.UseOpenIdConnectAuthentication(options); + }; + }); + + // Add framework services. + services.AddMvc(); + } + + // 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(); + } + else + { + app.UseExceptionHandler("/Home/Error"); + } + + app.UseStaticFiles(); + + app.UseAuthentication(); app.UseMvc(routes => { @@ -140,4 +118,4 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF }); } } -} +} \ No newline at end of file diff --git a/Quickstart/04-User-Profile/SampleMvcApp/appsettings.json b/Quickstart/04-User-Profile/SampleMvcApp/appsettings.json index 4c3e4b4..932c0e5 100644 --- a/Quickstart/04-User-Profile/SampleMvcApp/appsettings.json +++ b/Quickstart/04-User-Profile/SampleMvcApp/appsettings.json @@ -10,7 +10,6 @@ "Auth0": { "Domain": "{DOMAIN}", "ClientId": "{CLIENT_ID}", - "ClientSecret": "{CLIENT_SECRET}", - "CallbackUrl": "http://localhost:60856/signin-auth0" + "ClientSecret": "{CLIENT_SECRET}" } } diff --git a/Quickstart/05-Authorization/SampleMvcApp/Auth0Settings.cs b/Quickstart/05-Authorization/SampleMvcApp/Auth0Settings.cs deleted file mode 100644 index 1370f8e..0000000 --- a/Quickstart/05-Authorization/SampleMvcApp/Auth0Settings.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace SampleMvcApp -{ - public class Auth0Settings - { - public string Domain { get; set; } - - public string CallbackUrl { get; set; } - - public string ClientId { get; set; } - - public string ClientSecret { get; set; } - } -} diff --git a/Quickstart/05-Authorization/SampleMvcApp/Controllers/AccountController.cs b/Quickstart/05-Authorization/SampleMvcApp/Controllers/AccountController.cs index ca72755..5d4dcf0 100644 --- a/Quickstart/05-Authorization/SampleMvcApp/Controllers/AccountController.cs +++ b/Quickstart/05-Authorization/SampleMvcApp/Controllers/AccountController.cs @@ -1,32 +1,34 @@ -using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Authentication.OpenIdConnect; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Http.Authentication; using Microsoft.AspNetCore.Mvc; +using System.Threading.Tasks; using SampleMvcApp.ViewModels; using System.Linq; using System.Security.Claims; -using System.Threading.Tasks; namespace SampleMvcApp.Controllers { public class AccountController : Controller { - public IActionResult Login(string returnUrl = "/") + public async Task Login(string returnUrl = "/") { - return new ChallengeResult("Auth0", new AuthenticationProperties() { RedirectUri = returnUrl }); + await HttpContext.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, + new AuthenticationProperties() { RedirectUri = returnUrl }); } [Authorize] public async Task Logout() { - await HttpContext.Authentication.SignOutAsync("Auth0", new AuthenticationProperties + await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties { // Indicate here where Auth0 should redirect the user after a logout. // Note that the resulting absolute Uri must be whitelisted in the // **Allowed Logout URLs** settings for the client. RedirectUri = Url.Action("Index", "Home") }); - await HttpContext.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); + await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); } [Authorize] @@ -36,12 +38,11 @@ public IActionResult Profile() { Name = User.Identity.Name, EmailAddress = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email)?.Value, - ProfileImage = User.Claims.FirstOrDefault(c => c.Type == "picture")?.Value, - Country = User.Claims.FirstOrDefault(c => c.Type == "https://schemas.quickstarts.com/country")?.Value + ProfileImage = User.Claims.FirstOrDefault(c => c.Type == "picture")?.Value }); } - + /// /// This is just a helper action to enable you to easily see all claims related to a user. It helps when debugging your /// application to see the in claims populated from the Auth0 ID Token diff --git a/Quickstart/05-Authorization/SampleMvcApp/Program.cs b/Quickstart/05-Authorization/SampleMvcApp/Program.cs index 67afacb..b51f20f 100644 --- a/Quickstart/05-Authorization/SampleMvcApp/Program.cs +++ b/Quickstart/05-Authorization/SampleMvcApp/Program.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; namespace SampleMvcApp @@ -11,14 +7,12 @@ public class Program { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseIISIntegration() + BuildWebHost(args).Run(); + } + + public static IWebHost BuildWebHost(string[] args) => + WebHost.CreateDefaultBuilder(args) .UseStartup() .Build(); - - host.Run(); - } } } diff --git a/Quickstart/05-Authorization/SampleMvcApp/Project_Readme.html b/Quickstart/05-Authorization/SampleMvcApp/Project_Readme.html deleted file mode 100644 index 1a0f5b5..0000000 --- a/Quickstart/05-Authorization/SampleMvcApp/Project_Readme.html +++ /dev/null @@ -1,187 +0,0 @@ - - - - - Welcome to ASP.NET Core - - - - - - - - - - diff --git a/Quickstart/05-Authorization/SampleMvcApp/SampleMvcApp.csproj b/Quickstart/05-Authorization/SampleMvcApp/SampleMvcApp.csproj index 1d77c93..bb6c231 100644 --- a/Quickstart/05-Authorization/SampleMvcApp/SampleMvcApp.csproj +++ b/Quickstart/05-Authorization/SampleMvcApp/SampleMvcApp.csproj @@ -1,45 +1,18 @@  - - netcoreapp1.1 - true - SampleMvcApp - Exe - SampleMvcApp - 1.1.0 - $(PackageTargetFallback);dotnet5.6;portable-net45+win8 + netcoreapp2.0 + $(AssetTargetFallback);portable-net45+win8+wp8+wpa81; - - - PreserveNewest - + + - - - - - - - - - - - - - - - + + - - - - - - - + \ No newline at end of file diff --git a/Quickstart/05-Authorization/SampleMvcApp/Startup.cs b/Quickstart/05-Authorization/SampleMvcApp/Startup.cs index 0b17ed3..6b575f1 100644 --- a/Quickstart/05-Authorization/SampleMvcApp/Startup.cs +++ b/Quickstart/05-Authorization/SampleMvcApp/Startup.cs @@ -16,97 +16,60 @@ namespace SampleMvcApp { public class Startup { - public Startup(IHostingEnvironment env) + public Startup(IConfiguration configuration) { - var builder = new ConfigurationBuilder() - .SetBasePath(env.ContentRootPath) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) - .AddEnvironmentVariables(); - Configuration = builder.Build(); + Configuration = configuration; } - public IConfigurationRoot Configuration { get; } + 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) { // Add authentication services - services.AddAuthentication( - options => options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme); - - // Add framework services. - services.AddMvc(); - - // Add functionality to inject IOptions - services.AddOptions(); - - // Add the Auth0 Settings object so it can be injected - services.Configure(Configuration.GetSection("Auth0")); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IOptions auth0Settings) - { - loggerFactory.AddConsole(Configuration.GetSection("Logging")); - loggerFactory.AddDebug(); - - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - app.UseBrowserLink(); - } - else - { - app.UseExceptionHandler("/Home/Error"); - } - - app.UseStaticFiles(); - - // Add the cookie middleware - app.UseCookieAuthentication(new CookieAuthenticationOptions - { - AutomaticAuthenticate = true, - AutomaticChallenge = true - }); - - // Add the OIDC middleware - var options = new OpenIdConnectOptions("Auth0") - { + services.AddAuthentication(options => { + options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme; + options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; + }) + .AddCookieAuthentication() + .AddOpenIdConnectAuthentication(options => { // Set the authority to your Auth0 domain - Authority = $"https://{auth0Settings.Value.Domain}", + options.Authority = $"https://{Configuration["Auth0:Domain"]}"; // Configure the Auth0 Client ID and Client Secret - ClientId = auth0Settings.Value.ClientId, - ClientSecret = auth0Settings.Value.ClientSecret, - - // Do not automatically authenticate and challenge - AutomaticAuthenticate = false, - AutomaticChallenge = false, + options.ClientId = Configuration["Auth0:ClientId"]; + options.ClientSecret = Configuration["Auth0:ClientSecret"]; // Set response type to code - ResponseType = "code", + options.ResponseType = "code"; + + // Configure the scope + options.Scope.Clear(); + options.Scope.Add("openid"); + options.Scope.Add("profile"); + options.Scope.Add("email"); // Set the callback path, so Auth0 will call back to http://localhost:5000/signin-auth0 // Also ensure that you have added the URL as an Allowed Callback URL in your Auth0 dashboard - CallbackPath = new PathString("/signin-auth0"), + options.CallbackPath = new PathString("/signin-auth0"); // Configure the Claims Issuer to be Auth0 - ClaimsIssuer = "Auth0", + options.ClaimsIssuer = "Auth0"; // Set the correct name claim type - TokenValidationParameters = new TokenValidationParameters + options.TokenValidationParameters = new TokenValidationParameters { NameClaimType = "name", RoleClaimType = "https://schemas.quickstarts.com/roles" - }, + }; - Events = new OpenIdConnectEvents + options.Events = new OpenIdConnectEvents { // handle the logout redirection OnRedirectToIdentityProviderForSignOut = (context) => { - var logoutUri = $"https://{auth0Settings.Value.Domain}/v2/logout?client_id={auth0Settings.Value.ClientId}"; + var logoutUri = $"https://{Configuration["Auth0:Domain"]}/v2/logout?client_id={Configuration["Auth0:ClientId"]}"; var postLogoutUri = context.Properties.RedirectUri; if (!string.IsNullOrEmpty(postLogoutUri)) @@ -125,12 +88,28 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF return Task.CompletedTask; } - } - }; - options.Scope.Clear(); - options.Scope.Add("openid"); - options.Scope.Add("profile"); - app.UseOpenIdConnectAuthentication(options); + }; + }); + + // Add framework services. + services.AddMvc(); + } + + // 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(); + } + else + { + app.UseExceptionHandler("/Home/Error"); + } + + app.UseStaticFiles(); + + app.UseAuthentication(); app.UseMvc(routes => { @@ -140,4 +119,4 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF }); } } -} +} \ No newline at end of file diff --git a/Quickstart/05-Authorization/SampleMvcApp/Views/Account/Profile.cshtml b/Quickstart/05-Authorization/SampleMvcApp/Views/Account/Profile.cshtml index 0b0924e..4022d51 100644 --- a/Quickstart/05-Authorization/SampleMvcApp/Views/Account/Profile.cshtml +++ b/Quickstart/05-Authorization/SampleMvcApp/Views/Account/Profile.cshtml @@ -17,9 +17,6 @@

@Model.EmailAddress

-

- @Model.Country -

diff --git a/Quickstart/05-Authorization/SampleMvcApp/appsettings.json b/Quickstart/05-Authorization/SampleMvcApp/appsettings.json index f0f45c5..932c0e5 100644 --- a/Quickstart/05-Authorization/SampleMvcApp/appsettings.json +++ b/Quickstart/05-Authorization/SampleMvcApp/appsettings.json @@ -10,7 +10,6 @@ "Auth0": { "Domain": "{DOMAIN}", "ClientId": "{CLIENT_ID}", - "ClientSecret": "{CLIENT_SECRET}", - "CallbackUrl": "http://localhost:5000/signin-auth0" + "ClientSecret": "{CLIENT_SECRET}" } }