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:
@@ -1,7 +0,0 @@
|
||||
namespace BlazorAdmin
|
||||
{
|
||||
public class Constants
|
||||
{
|
||||
public const string API_URL = "https://localhost:5099/api/";
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@
|
||||
{
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<img class="col-md-6 esh-picture" src="@($"https://localhost:44315/{_item.PictureUri}")">
|
||||
<img class="col-md-6 esh-picture" src="@($"{Auth.WebUrl}{_item.PictureUri}")">
|
||||
|
||||
<dl class="col-md-6 dl-horizontal">
|
||||
<dt>
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
{
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<img class="col-md-6 esh-picture" src="@($"https://localhost:44315/{_item.PictureUri}")">
|
||||
<img class="col-md-6 esh-picture" src="@($"{Auth.WebUrl}{_item.PictureUri}")">
|
||||
|
||||
<dl class="col-md-6 dl-horizontal">
|
||||
<dt>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
@page "/admin"
|
||||
@attribute [Authorize(Roles = BlazorShared.Authorization.Constants.Roles.ADMINISTRATORS)]
|
||||
@inject AuthService Auth
|
||||
@using global::BlazorShared.Authorization
|
||||
@inherits BlazorAdmin.Helpers.BlazorComponent
|
||||
@namespace BlazorAdmin.Pages.CatalogItemPage
|
||||
|
||||
@@ -38,7 +37,7 @@ else
|
||||
{
|
||||
<tr @onclick="@(() => DetailsClick(item.Id))">
|
||||
<td>
|
||||
<img class="img-thumbnail" src="@($"https://localhost:44315/{item.PictureUri}")">
|
||||
<img class="img-thumbnail" src="@($"{Auth.WebUrl}{item.PictureUri}")">
|
||||
</td>
|
||||
<td>@Services.CatalogTypeServices.List.GetTypeName(catalogTypes, item.CatalogTypeId)</td>
|
||||
<td>@Services.CatalogBrandServices.List.GetBrandName(catalogBrands, item.CatalogBrandId)</td>
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Http.Json;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using BlazorAdmin.JavaScript;
|
||||
@@ -20,6 +16,12 @@ namespace BlazorAdmin.Services
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly ILocalStorageService _localStorage;
|
||||
private readonly IJSRuntime _jSRuntime;
|
||||
|
||||
public string ApiUrl => Constants.GetApiUrl(InDocker);
|
||||
public string WebUrl => Constants.GetWebUrl(InDocker);
|
||||
|
||||
private static bool InDocker { get; set; }
|
||||
|
||||
public bool IsLoggedIn { get; set; }
|
||||
public string UserName { get; set; }
|
||||
|
||||
@@ -30,51 +32,33 @@ namespace BlazorAdmin.Services
|
||||
_jSRuntime = jSRuntime;
|
||||
}
|
||||
|
||||
public HttpClient GetHttpClient()
|
||||
public async Task<HttpResponseMessage> HttpGet(string uri)
|
||||
{
|
||||
return _httpClient;
|
||||
return await _httpClient.GetAsync($"{ApiUrl}{uri}");
|
||||
}
|
||||
|
||||
public async Task<AuthResponse> LoginWithoutSaveToLocalStorage(AuthRequest user)
|
||||
public async Task<HttpResponseMessage> HttpDelete(string uri, int id)
|
||||
{
|
||||
var jsonContent = new StringContent(JsonConvert.SerializeObject(user), Encoding.UTF8, "application/json");
|
||||
var response = await _httpClient.PostAsync($"{Constants.API_URL}authenticate", jsonContent);
|
||||
var authResponse = new AuthResponse();
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
authResponse = await DeserializeToAuthResponse(response);
|
||||
|
||||
IsLoggedIn = true;
|
||||
}
|
||||
|
||||
return authResponse;
|
||||
return await _httpClient.DeleteAsync($"{ApiUrl}{uri}/{id}");
|
||||
}
|
||||
|
||||
public async Task<AuthResponse> Login(AuthRequest user)
|
||||
public async Task<HttpResponseMessage> HttpPost(string uri, object dataToSend)
|
||||
{
|
||||
var jsonContent = new StringContent(JsonConvert.SerializeObject(user), Encoding.UTF8, "application/json");
|
||||
var response = await _httpClient.PostAsync($"{Constants.API_URL}authenticate", jsonContent);
|
||||
var authResponse = new AuthResponse();
|
||||
var content = ToJson(dataToSend);
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
authResponse = await DeserializeToAuthResponse(response);
|
||||
await SaveTokenInLocalStorage(authResponse);
|
||||
await SaveUsernameInLocalStorage(authResponse);
|
||||
await SetAuthorizationHeader();
|
||||
return await _httpClient.PostAsync($"{ApiUrl}{uri}", content);
|
||||
}
|
||||
|
||||
UserName = await GetUsername();
|
||||
IsLoggedIn = true;
|
||||
}
|
||||
public async Task<HttpResponseMessage> HttpPut(string uri, object dataToSend)
|
||||
{
|
||||
var content = ToJson(dataToSend);
|
||||
|
||||
return authResponse;
|
||||
return await _httpClient.PutAsync($"{ApiUrl}{uri}", content);
|
||||
}
|
||||
|
||||
public async Task Logout()
|
||||
{
|
||||
await _localStorage.RemoveItemAsync("authToken");
|
||||
await _localStorage.RemoveItemAsync("username");
|
||||
await DeleteLocalStorage();
|
||||
await DeleteCookies();
|
||||
RemoveAuthorizationHeader();
|
||||
UserName = null;
|
||||
@@ -95,67 +79,11 @@ namespace BlazorAdmin.Services
|
||||
var username = await new Cookies(_jSRuntime).GetCookie("username");
|
||||
await SaveUsernameInLocalStorage(username);
|
||||
|
||||
var inDocker = await new Cookies(_jSRuntime).GetCookie("inDocker");
|
||||
await SaveInDockerInLocalStorage(inDocker);
|
||||
|
||||
await RefreshLoginInfo();
|
||||
}
|
||||
private async Task LogoutIdentityManager()
|
||||
{
|
||||
await _httpClient.PostAsync("Identity/Account/Logout", null);
|
||||
}
|
||||
|
||||
private async Task DeleteCookies()
|
||||
{
|
||||
await new Cookies(_jSRuntime).DeleteCookie("token");
|
||||
await new Cookies(_jSRuntime).DeleteCookie("username");
|
||||
}
|
||||
|
||||
private async Task SetLoginData()
|
||||
{
|
||||
IsLoggedIn = !string.IsNullOrEmpty(await GetToken());
|
||||
UserName = await GetUsername();
|
||||
await SetAuthorizationHeader();
|
||||
}
|
||||
|
||||
private async Task<AuthResponse> DeserializeToAuthResponse(HttpResponseMessage response)
|
||||
{
|
||||
var responseContent = await response.Content.ReadAsStringAsync();
|
||||
return JsonConvert.DeserializeObject<AuthResponse>(responseContent);
|
||||
}
|
||||
|
||||
private async Task SaveTokenInLocalStorage(AuthResponse authResponse)
|
||||
{
|
||||
await _localStorage.SetItemAsync("authToken", SaveTokenInLocalStorage(authResponse.Token));
|
||||
}
|
||||
|
||||
private async Task SaveTokenInLocalStorage(string token)
|
||||
{
|
||||
if (string.IsNullOrEmpty(token))
|
||||
{
|
||||
return;
|
||||
}
|
||||
await _localStorage.SetItemAsync("authToken", token);
|
||||
}
|
||||
|
||||
private void RemoveAuthorizationHeader()
|
||||
{
|
||||
if (_httpClient.DefaultRequestHeaders.Contains("Authorization"))
|
||||
{
|
||||
_httpClient.DefaultRequestHeaders.Remove("Authorization");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SaveUsernameInLocalStorage(AuthResponse authResponse)
|
||||
{
|
||||
await _localStorage.SetItemAsync("username", SaveUsernameInLocalStorage(authResponse.Username));
|
||||
}
|
||||
|
||||
private async Task SaveUsernameInLocalStorage(string username)
|
||||
{
|
||||
if (string.IsNullOrEmpty(username))
|
||||
{
|
||||
return;
|
||||
}
|
||||
await _localStorage.SetItemAsync("username", username);
|
||||
}
|
||||
|
||||
public async Task<string> GetToken()
|
||||
{
|
||||
@@ -175,58 +103,83 @@ namespace BlazorAdmin.Services
|
||||
return username;
|
||||
}
|
||||
|
||||
public async Task<bool> GetInDocker()
|
||||
{
|
||||
return (await _localStorage.GetItemAsync<string>("inDocker")).ToLower() == "true";
|
||||
}
|
||||
|
||||
private StringContent ToJson(object obj)
|
||||
{
|
||||
return new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json");
|
||||
}
|
||||
|
||||
private async Task LogoutIdentityManager()
|
||||
{
|
||||
await _httpClient.PostAsync("Identity/Account/Logout", null);
|
||||
}
|
||||
|
||||
private async Task DeleteLocalStorage()
|
||||
{
|
||||
await _localStorage.RemoveItemAsync("authToken");
|
||||
await _localStorage.RemoveItemAsync("username");
|
||||
await _localStorage.RemoveItemAsync("inDocker");
|
||||
}
|
||||
|
||||
private async Task DeleteCookies()
|
||||
{
|
||||
await new Cookies(_jSRuntime).DeleteCookie("token");
|
||||
await new Cookies(_jSRuntime).DeleteCookie("username");
|
||||
await new Cookies(_jSRuntime).DeleteCookie("inDocker");
|
||||
}
|
||||
|
||||
private async Task SetLoginData()
|
||||
{
|
||||
IsLoggedIn = !string.IsNullOrEmpty(await GetToken());
|
||||
UserName = await GetUsername();
|
||||
InDocker = await GetInDocker();
|
||||
await SetAuthorizationHeader();
|
||||
}
|
||||
|
||||
private void RemoveAuthorizationHeader()
|
||||
{
|
||||
if (_httpClient.DefaultRequestHeaders.Contains("Authorization"))
|
||||
{
|
||||
_httpClient.DefaultRequestHeaders.Remove("Authorization");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SaveTokenInLocalStorage(string token)
|
||||
{
|
||||
if (string.IsNullOrEmpty(token))
|
||||
{
|
||||
return;
|
||||
}
|
||||
await _localStorage.SetItemAsync("authToken", token);
|
||||
}
|
||||
|
||||
private async Task SaveUsernameInLocalStorage(string username)
|
||||
{
|
||||
if (string.IsNullOrEmpty(username))
|
||||
{
|
||||
return;
|
||||
}
|
||||
await _localStorage.SetItemAsync("username", username);
|
||||
}
|
||||
|
||||
private async Task SaveInDockerInLocalStorage(string inDocker)
|
||||
{
|
||||
if (string.IsNullOrEmpty(inDocker))
|
||||
{
|
||||
return;
|
||||
}
|
||||
await _localStorage.SetItemAsync("inDocker", inDocker);
|
||||
}
|
||||
|
||||
private async Task SetAuthorizationHeader()
|
||||
{
|
||||
var token = await GetToken();
|
||||
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
||||
}
|
||||
|
||||
public IEnumerable<Claim> ParseClaimsFromJwt(string jwt)
|
||||
{
|
||||
var claims = new List<Claim>();
|
||||
if (string.IsNullOrEmpty(jwt))
|
||||
{
|
||||
return claims;
|
||||
}
|
||||
|
||||
var payload = jwt.Split('.')[1];
|
||||
var jsonBytes = ParseBase64WithoutPadding(payload);
|
||||
var keyValuePairs = JsonConvert.DeserializeObject<Dictionary<string, object>>(Encoding.UTF8.GetString(jsonBytes));
|
||||
|
||||
keyValuePairs.TryGetValue(ClaimTypes.Role, out object roles);
|
||||
|
||||
if (roles != null)
|
||||
{
|
||||
if (roles.ToString().Trim().StartsWith("["))
|
||||
{
|
||||
var parsedRoles = JsonConvert.DeserializeObject<string[]>(roles.ToString());
|
||||
|
||||
foreach (var parsedRole in parsedRoles)
|
||||
{
|
||||
claims.Add(new Claim(ClaimTypes.Role, parsedRole));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
claims.Add(new Claim(ClaimTypes.Role, roles.ToString()));
|
||||
}
|
||||
|
||||
keyValuePairs.Remove(ClaimTypes.Role);
|
||||
}
|
||||
|
||||
claims.AddRange(keyValuePairs.Select(kvp => new Claim(kvp.Key, kvp.Value.ToString())));
|
||||
|
||||
return claims;
|
||||
}
|
||||
|
||||
private byte[] ParseBase64WithoutPadding(string base64)
|
||||
{
|
||||
switch (base64.Length % 4)
|
||||
{
|
||||
case 2: base64 += "=="; break;
|
||||
case 3: base64 += "="; break;
|
||||
}
|
||||
return Convert.FromBase64String(base64);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ namespace BlazorAdmin.Services.CatalogBrandServices
|
||||
|
||||
try
|
||||
{
|
||||
var result = (await _authService.GetHttpClient().GetAsync($"{Constants.API_URL}catalog-brands"));
|
||||
var result = await _authService.HttpGet("catalog-brands");
|
||||
if (result.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
return brands;
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
@@ -19,9 +17,7 @@ namespace BlazorAdmin.Services.CatalogItemServices
|
||||
{
|
||||
var catalogItemResult = new CatalogItem();
|
||||
|
||||
var content = new StringContent(JsonConvert.SerializeObject(catalogItem), Encoding.UTF8, "application/json");
|
||||
|
||||
var result = await _authService.GetHttpClient().PostAsync($"{Constants.API_URL}catalog-items", content);
|
||||
var result = await _authService.HttpPost("catalog-items", catalogItem);
|
||||
if (result.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
return catalogItemResult;
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace BlazorAdmin.Services.CatalogItemServices
|
||||
{
|
||||
var catalogItemResult = string.Empty;
|
||||
|
||||
var result = await _authService.GetHttpClient().DeleteAsync($"{Constants.API_URL}catalog-items/{catalogItemId}");
|
||||
var result = await _authService.HttpDelete("catalog-items", catalogItemId);
|
||||
if (result.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
return catalogItemResult;
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
@@ -19,9 +17,7 @@ namespace BlazorAdmin.Services.CatalogItemServices
|
||||
{
|
||||
var catalogItemResult = new CatalogItem();
|
||||
|
||||
var content = new StringContent(JsonConvert.SerializeObject(catalogItem), Encoding.UTF8, "application/json");
|
||||
|
||||
var result = await _authService.GetHttpClient().PutAsync($"{Constants.API_URL}catalog-items", content);
|
||||
var result = await _authService.HttpPut("catalog-items", catalogItem);
|
||||
if (result.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
return catalogItemResult;
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace BlazorAdmin.Services.CatalogItemServices
|
||||
{
|
||||
var catalogItemResult = new CatalogItem();
|
||||
|
||||
var result = await _authService.GetHttpClient().GetAsync($"{Constants.API_URL}catalog-items/{catalogItemId}");
|
||||
var result = await _authService.HttpGet($"catalog-items/{catalogItemId}");
|
||||
if (result.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
return catalogItemResult;
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace BlazorAdmin.Services.CatalogItemServices
|
||||
{
|
||||
var catalogItems = new List<CatalogItem>();
|
||||
|
||||
var result = (await _authService.GetHttpClient().GetAsync($"{Constants.API_URL}catalog-items?PageSize={pageSize}"));
|
||||
var result = await _authService.HttpGet($"catalog-items?PageSize={pageSize}");
|
||||
if (result.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
return catalogItems;
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace BlazorAdmin.Services.CatalogTypeServices
|
||||
|
||||
try
|
||||
{
|
||||
var result = (await _authService.GetHttpClient().GetAsync($"{Constants.API_URL}catalog-types"));
|
||||
var result = await _authService.HttpGet("catalog-types");
|
||||
if (result.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
return types;
|
||||
|
||||
@@ -27,12 +27,6 @@
|
||||
<span class="oi oi-account-logout" aria-hidden="true"></span> Logout
|
||||
</NavLink>
|
||||
}
|
||||
else
|
||||
{
|
||||
<NavLink class="nav-link" href="login">
|
||||
<span class="oi oi-account-login" aria-hidden="true"></span> Login
|
||||
</NavLink>
|
||||
}
|
||||
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
@@ -4,6 +4,6 @@
|
||||
protected override void OnInitialized()
|
||||
{
|
||||
Navigation.NavigateTo($"Identity/Account/Login?returnUrl=" +
|
||||
Uri.EscapeDataString(Navigation.Uri));
|
||||
$"/{Uri.EscapeDataString(Navigation.ToBaseRelativePath(Navigation.Uri))}");
|
||||
}
|
||||
}
|
||||
@@ -7,11 +7,12 @@
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@using Microsoft.AspNetCore.Components.WebAssembly.Http
|
||||
@using Microsoft.JSInterop
|
||||
@using Microsoft.Extensions.Logging
|
||||
@using BlazorAdmin
|
||||
@using BlazorAdmin.Shared
|
||||
@using BlazorAdmin.Services
|
||||
@using BlazorAdmin.Services.CatalogBrandServices
|
||||
@using BlazorAdmin.Services.CatalogItemServices
|
||||
@using BlazorAdmin.Services.CatalogTypeServices
|
||||
@using Microsoft.Extensions.Logging
|
||||
@using BlazorAdmin.JavaScript
|
||||
@using BlazorShared.Authorization
|
||||
|
||||
Reference in New Issue
Block a user