Docker Fix (#431)

* static added to Constants

* Docker support for Blazor

* GetHttp, PostHttp, ... inside AuthService, Docker working with login, Cookies Configuration temporary disabled

* BaseAddress get web uri from Blazor Shared.

* cookie options changed to fix docker.

* Fixed returnUrl when inserting admin link and navigate without login

* Functions not used removed.

* AddPolicy using GetWebUrl

* Login link removed from NavMenu

* Change ConfigureCookieSettings, ConfigureCoreServices and ConfigureWebServices to be IServiceCollection extentions.

* GetOriginWebUrl added.

* Auto InDocker switch added.

* Removed not used using .
This commit is contained in:
Shady Nagy
2020-07-27 15:06:18 +02:00
committed by GitHub
parent e1f9ddd192
commit 688064199d
25 changed files with 172 additions and 215 deletions

View File

@@ -89,7 +89,7 @@ namespace Microsoft.eShopWeb.Web.Areas.Identity.Pages.Account
if (result.Succeeded)
{
var token = await _tokenClaimsService.GetTokenAsync(Input.Email);
CreateAuthCookie(Input.Email, token);
CreateAuthCookie(Input.Email, token, Startup.InDocker);
_logger.LogInformation("User logged in.");
await TransferAnonymousBasketToUserAsync(Input.Email);
return LocalRedirect(returnUrl);
@@ -114,12 +114,13 @@ namespace Microsoft.eShopWeb.Web.Areas.Identity.Pages.Account
return Page();
}
private void CreateAuthCookie(string username, string token)
private void CreateAuthCookie(string username, string token, bool inDocker)
{
var cookieOptions = new CookieOptions();
cookieOptions.Expires = DateTime.Today.AddYears(10);
Response.Cookies.Append("token", token, cookieOptions);
Response.Cookies.Append("username", username, cookieOptions);
Response.Cookies.Append("inDocker", inDocker.ToString(), cookieOptions);
}
private async Task TransferAnonymousBasketToUserAsync(string userName)

View File

@@ -7,14 +7,14 @@ namespace Microsoft.eShopWeb.Web.Configuration
{
public static class ConfigureCookieSettings
{
public static void Configure(IServiceCollection services)
public static IServiceCollection AddCookieSettings(this IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
//TODO need to check that.
//options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
options.MinimumSameSitePolicy = SameSiteMode.Strict;
});
services.ConfigureApplicationCookie(options =>
{
@@ -27,6 +27,8 @@ namespace Microsoft.eShopWeb.Web.Configuration
IsEssential = true // required for auth to work without explicit user consent; adjust to suit your privacy policy
};
});
return services;
}
}
}

View File

@@ -10,7 +10,7 @@ namespace Microsoft.eShopWeb.Web.Configuration
{
public static class ConfigureCoreServices
{
public static void Configure(IServiceCollection services, IConfiguration configuration)
public static IServiceCollection AddCoreServices(this IServiceCollection services, IConfiguration configuration)
{
services.AddScoped(typeof(IAsyncRepository<>), typeof(EfRepository<>));
services.AddScoped<IBasketService, BasketService>();
@@ -19,6 +19,8 @@ namespace Microsoft.eShopWeb.Web.Configuration
services.AddSingleton<IUriComposer>(new UriComposer(configuration.Get<CatalogSettings>()));
services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>));
services.AddTransient<IEmailSender, EmailSender>();
return services;
}
}
}

View File

@@ -8,7 +8,7 @@ namespace Microsoft.eShopWeb.Web.Configuration
{
public static class ConfigureWebServices
{
public static void Configure(IServiceCollection services, IConfiguration configuration)
public static IServiceCollection AddWebServices(this IServiceCollection services, IConfiguration configuration)
{
services.AddMediatR(typeof(BasketViewModelService).Assembly);
services.AddScoped<IBasketViewModelService, BasketViewModelService>();
@@ -16,6 +16,8 @@ namespace Microsoft.eShopWeb.Web.Configuration
services.AddScoped<ICatalogItemViewModelService, CatalogItemViewModelService>();
services.Configure<CatalogSettings>(configuration);
services.AddScoped<ICatalogViewModelService, CachedCatalogViewModelService>();
return services;
}
}
}

View File

@@ -15,14 +15,14 @@ using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Mime;
using BlazorAdmin.Services;
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.eShopWeb.ApplicationCore.Interfaces;
namespace Microsoft.eShopWeb.Web
@@ -30,6 +30,8 @@ namespace Microsoft.eShopWeb.Web
public class Startup
{
private IServiceCollection _services;
public static bool InDocker => Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER") == "true";
public Startup(IConfiguration configuration)
{
Configuration = configuration;
@@ -83,7 +85,22 @@ namespace Microsoft.eShopWeb.Web
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
ConfigureCookieSettings.Configure(services);
services.AddCookieSettings();
if (InDocker)
{
services.AddDataProtection()
.SetApplicationName("eshopwebmvc")
.PersistKeysToFileSystem(new DirectoryInfo(@"./"));
}
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.Cookie.HttpOnly = true;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
options.Cookie.SameSite = SameSiteMode.Lax;
});
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddDefaultUI()
@@ -92,8 +109,8 @@ namespace Microsoft.eShopWeb.Web
services.AddScoped<ITokenClaimsService, IdentityTokenClaimService>();
ConfigureCoreServices.Configure(services, Configuration);
ConfigureWebServices.Configure(services, Configuration);
services.AddCoreServices(Configuration);
services.AddWebServices(Configuration);
// Add memory cache services
services.AddMemoryCache();
@@ -124,15 +141,9 @@ namespace Microsoft.eShopWeb.Web
});
// Blazor Admin Required Services for Prerendering
services.AddScoped<HttpClient>(s =>
services.AddScoped<HttpClient>(s => new HttpClient
{
var navigationManager = s.GetRequiredService<NavigationManager>();
return new HttpClient
{
//TODO need to do it well
BaseAddress = new Uri("https://localhost:44315/")
//BaseAddress = new Uri(navigationManager.BaseUri)
};
BaseAddress = new Uri(BlazorShared.Authorization.Constants.GetWebUrl(InDocker))
});
services.AddBlazoredLocalStorage();
@@ -197,6 +208,7 @@ namespace Microsoft.eShopWeb.Web
endpoints.MapFallbackToFile("index.html");
});
}
}
}