je suis pas encore sur de comment faire pour lié l'authentification au projet principales
continuous-integration/drone/push Build is failing Details

blazor
Patrick BRUGIERE 1 year ago
parent 4d11e05410
commit 597baaccba

@ -9,6 +9,7 @@
<PropertyGroup Condition=" '$(RunConfiguration)' == 'Minecraft.Crafting.Api' " />
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

@ -4,9 +4,14 @@ Microsoft Visual Studio Solution File, Format Version 12.00
VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "adminBlazor", "adminBlazor\adminBlazor.csproj", "{5985AE7A-DA9C-4A68-80E0-2D92926C1FB5}"
ProjectSection(ProjectDependencies) = postProject
{7A7CE974-02E6-4248-B5C5-54A04724327B} = {7A7CE974-02E6-4248-B5C5-54A04724327B}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "adminBlazor.Api", "adminBlazor.Api\adminBlazor.Api.csproj", "{69C7F430-9B80-4253-B572-FDBC90076C5E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DemoAuthentication", "..\..\..\..\..\Downloads\Authentication\DemoAuthentication\DemoAuthentication\DemoAuthentication.csproj", "{7A7CE974-02E6-4248-B5C5-54A04724327B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -21,6 +26,10 @@ Global
{69C7F430-9B80-4253-B572-FDBC90076C5E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69C7F430-9B80-4253-B572-FDBC90076C5E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69C7F430-9B80-4253-B572-FDBC90076C5E}.Release|Any CPU.Build.0 = Release|Any CPU
{7A7CE974-02E6-4248-B5C5-54A04724327B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7A7CE974-02E6-4248-B5C5-54A04724327B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A7CE974-02E6-4248-B5C5-54A04724327B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A7CE974-02E6-4248-B5C5-54A04724327B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -1,4 +1,4 @@
<CascadingBlazoredModal>

<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
@ -10,5 +10,4 @@
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingBlazoredModal>
</Router>

@ -1,9 +0,0 @@
namespace adminBlazor.Models
{
public class AppUser
{
public string Password { get; set; }
public List<string> Roles { get; set; }
public string UserName { get; set; }
}
}

@ -1,9 +0,0 @@
namespace adminBlazor.Models
{
public class CurrentUser
{
public Dictionary<string, string> Claims { get; set; }
public bool IsAuthenticated { get; set; }
public string UserName { get; set; }
}
}

@ -1,13 +0,0 @@
using System.ComponentModel.DataAnnotations;
namespace adminBlazor.Models
{
public class LoginRequest
{
[Required]
public string Password { get; set; }
[Required]
public string UserName { get; set; }
}
}

@ -1,17 +0,0 @@
using System.ComponentModel.DataAnnotations;
namespace adminBlazor.Models
{
public class RegisterRequest
{
[Required]
public string Password { get; set; }
[Required]
[Compare(nameof(Password), ErrorMessage = "Passwords do not match!")]
public string PasswordConfirm { get; set; }
[Required]
public string UserName { get; set; }
}
}

@ -1,27 +0,0 @@
@page "/login"
@layout AuthLayout
<h1 class="h2 font-weight-normal login-title">
Login
</h1>
<EditForm class="form-signin" OnValidSubmit="OnSubmit" Model="loginRequest">
<DataAnnotationsValidator />
<label for="inputUsername" class="sr-only">User Name</label>
<InputText id="inputUsername" class="form-control" @bind-Value="loginRequest.UserName" autofocus placeholder="Username" />
<ValidationMessage For="@(() => loginRequest.UserName)" />
<label for="inputPassword" class="sr-only">Password</label>
<InputText type="password" id="inputPassword" class="form-control" placeholder="Password" @bind-Value="loginRequest.Password" />
<ValidationMessage For="@(() => loginRequest.Password)" />
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<label class="text-danger">@error</label>
<NavLink href="register">
<h6 class="font-weight-normal text-center">Create account</h6>
</NavLink>
</EditForm>

@ -1,32 +0,0 @@
using adminBlazor.Models;
using adminBlazor.Services;
using Microsoft.AspNetCore.Components;
namespace adminBlazor.Pages.Authentication
{
public partial class Login
{
[Inject]
public CustomStateProvider AuthStateProvider { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
private string error { get; set; }
private LoginRequest loginRequest { get; set; } = new LoginRequest();
private async Task OnSubmit()
{
error = null;
try
{
await AuthStateProvider.Login(loginRequest);
NavigationManager.NavigateTo("");
}
catch (Exception ex)
{
error = ex.Message;
}
}
}
}

@ -1,29 +0,0 @@
@page "/register"
@layout AuthLayout
<h1 class="h2 font-weight-normal login-title">
Register
</h1>
<EditForm class="form-signin" OnValidSubmit="OnSubmit" Model="registerRequest">
<DataAnnotationsValidator />
<label for="inputUsername" class="sr-only">User Name</label>
<InputText id="inputUsername" class="form-control" placeholder="Username" autofocus @bind-Value="@registerRequest.UserName" />
<ValidationMessage For="@(() => registerRequest.UserName)" />
<label for="inputPassword" class="sr-only">Password</label>
<InputText type="password" id="inputPassword" class="form-control" placeholder="Password" @bind-Value="@registerRequest.Password" />
<ValidationMessage For="@(() => registerRequest.Password)" />
<label for="inputPasswordConfirm" class="sr-only">Password Confirmation</label>
<InputText type="password" id="inputPasswordConfirm" class="form-control" placeholder="Password Confirmation" @bind-Value="@registerRequest.PasswordConfirm" />
<ValidationMessage For="@(() => registerRequest.PasswordConfirm)" />
<button class="btn btn-lg btn-primary btn-block" type="submit">Create account</button>
<label class="text-danger">@error</label>
<NavLink href="login">
<h6 class="font-weight-normal text-center">Already have an account? Click here to login</h6>
</NavLink>
</EditForm>

@ -1,32 +0,0 @@
using adminBlazor.Models;
using adminBlazor.Services;
using Microsoft.AspNetCore.Components;
namespace adminBlazor.Pages.Authentication
{
public partial class Register
{
[Inject]
public CustomStateProvider AuthStateProvider { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
private string error { get; set; }
private RegisterRequest registerRequest { get; set; } = new RegisterRequest();
private async Task OnSubmit()
{
error = null;
try
{
await AuthStateProvider.Register(registerRequest);
NavigationManager.NavigateTo("");
}
catch (Exception ex)
{
error = ex.Message;
}
}
}
}

@ -11,4 +11,5 @@
<p>
<b>CurrentCulture</b>: @CultureInfo.CurrentCulture
</p>
</p>

@ -12,13 +12,18 @@ using System.Globalization;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using adminBlazor.Models;
using adminBlazor;
using Microsoft.AspNetCore.Components.Authorization;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>();
@ -94,4 +99,3 @@ builder.Services.AddControllers();
app.Run();

@ -1,53 +0,0 @@
using adminBlazor.Models;
using System.Security.Claims;
namespace adminBlazor.Services
{
public class AuthService : IAuthService
{
private static readonly List<AppUser> CurrentUser;
static AuthService()
{
CurrentUser = new List<AppUser>
{
new AppUser { UserName = "Admin", Password = "123456", Roles = new List<string> { "admin" } }
};
}
public CurrentUser GetUser(string userName)
{
var user = CurrentUser.FirstOrDefault(w => w.UserName == userName);
if (user == null)
{
throw new Exception("User name or password invalid !");
}
var claims = new List<Claim>();
claims.AddRange(user.Roles.Select(s => new Claim(ClaimTypes.Role, s)));
return new CurrentUser
{
IsAuthenticated = true,
UserName = user.UserName,
Claims = claims.ToDictionary(c => c.Type, c => c.Value)
};
}
public void Login(LoginRequest loginRequest)
{
var user = CurrentUser.FirstOrDefault(w => w.UserName == loginRequest.UserName && w.Password == loginRequest.Password);
if (user == null)
{
throw new Exception("User name or password invalid !");
}
}
public void Register(RegisterRequest registerRequest)
{
CurrentUser.Add(new AppUser { UserName = registerRequest.UserName, Password = registerRequest.Password, Roles = new List<string> { "guest" } });
}
}
}

@ -1,75 +0,0 @@
using adminBlazor.Models;
using Microsoft.AspNetCore.Components.Authorization;
using System.Security.Claims;
namespace adminBlazor.Services
{
public class CustomStateProvider : AuthenticationStateProvider
{
private readonly IAuthService _authService;
private CurrentUser _currentUser;
public CustomStateProvider(IAuthService authService)
{
this._authService = authService;
}
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var identity = new ClaimsIdentity();
try
{
var userInfo = GetCurrentUser();
if (userInfo.IsAuthenticated)
{
var claims = new[] { new Claim(ClaimTypes.Name, _currentUser.UserName) }.Concat(_currentUser.Claims.Select(c => new Claim(c.Key, c.Value)));
identity = new ClaimsIdentity(claims, "Server authentication");
}
}
catch (HttpRequestException ex)
{
Console.WriteLine("Request failed:" + ex);
}
return new AuthenticationState(new ClaimsPrincipal(identity));
}
public async Task Login(LoginRequest loginParameters)
{
_authService.Login(loginParameters);
// No error - Login the user
var user = _authService.GetUser(loginParameters.UserName);
_currentUser = user;
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
}
public async Task Logout()
{
_currentUser = null;
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
}
public async Task Register(RegisterRequest registerParameters)
{
_authService.Register(registerParameters);
// No error - Login the user
var user = _authService.GetUser(registerParameters.UserName);
_currentUser = user;
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
}
private CurrentUser GetCurrentUser()
{
if (_currentUser != null && _currentUser.IsAuthenticated)
{
return _currentUser;
}
return new CurrentUser();
}
}
}

@ -1,13 +0,0 @@
using adminBlazor.Models;
namespace adminBlazor.Services
{
public interface IAuthService
{
CurrentUser GetUser(string userName);
void Login(LoginRequest loginRequest);
void Register(RegisterRequest registerRequest);
}
}

@ -1,6 +0,0 @@
@inherits LayoutComponentBase
<div class="main">
<div class="content px-4">
@Body
</div>
</div>

@ -10,6 +10,7 @@
</div>
</div>
<main>
<article class="content px-4">

@ -11,4 +11,4 @@
@using Blazorise.DataGrid
@using adminBlazor.Services
@using Blazored.Modal
@using Blazored.Modal.Services
@using Blazored.Modal.Services

@ -16,6 +16,7 @@
<PackageReference Include="Blazorise.DataGrid" Version="1.4.0" />
<PackageReference Include="Blazorise.Icons.FontAwesome" Version="1.4.0" />
<PackageReference Include="Blazored.Modal" Version="7.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="8.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
</ItemGroup>
<ItemGroup>

Loading…
Cancel
Save