Advertisement
RotundMidget

login

Mar 16th, 2023
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | Software | 0 0
  1. @page "/login"
  2. @using Microsoft.AspNetCore.Components.Authorization
  3. @inject AuthenticationStateProvider AuthenticationStateProvider
  4. @inject NavigationManager NavigationManager
  5.  
  6. <h3>Login</h3>
  7.  
  8. <EditForm Model="loginModel" OnValidSubmit="HandleValidSubmit">
  9.     <DataAnnotationsValidator />
  10.  
  11.     <div class="form-group">
  12.         <label for="email">Email:</label>
  13.         <InputText id="email" class="form-control" @bind-Value="loginModel.Email" />
  14.         <ValidationMessage For="@(() => loginModel.Email)" />
  15.     </div>
  16.  
  17.     <div class="form-group">
  18.         <label for="password">Password:</label>
  19.         <InputText id="password" class="form-control" type="password" @bind-Value="loginModel.Password" />
  20.         <ValidationMessage For="@(() => loginModel.Password)" />
  21.     </div>
  22.  
  23.     <button type="submit" class="btn btn-primary">Login</button>
  24.     @if (error != null)
  25.     {
  26.         <div class="alert alert-danger mt-3" role="alert">
  27.             @error
  28.         </div>
  29.     }
  30. </EditForm>
  31.  
  32. @code {
  33.     private LoginModel loginModel = new LoginModel();
  34.     private string error;
  35.  
  36.     // Replace with the authentication logic for your application
  37.     private async Task HandleValidSubmit()
  38.     {
  39.         // Authenticate the user with your existing authentication system.
  40.         var isAuthenticated = await AuthenticateUser(loginModel.Email, loginModel.Password);
  41.  
  42.         if (isAuthenticated)
  43.         {
  44.             var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
  45.             if (authState.User.Identity.IsAuthenticated)
  46.             {
  47.                 NavigationManager.NavigateTo("/");
  48.             }
  49.         }
  50.         else
  51.         {
  52.             error = "Invalid email or password";
  53.         }
  54.     }
  55.  
  56.     private async Task<bool> AuthenticateUser(string email, string password)
  57.     {
  58.         // Replace with your authentication logic
  59.         // Authenticate the user using your existing authentication system
  60.         // and return true if the user is authenticated, false otherwise.
  61.         return false;
  62.     }
  63.  
  64.     public class LoginModel
  65.     {
  66.         [Required, EmailAddress]
  67.         public string Email { get; set; }
  68.  
  69.         [Required, MinLength(6)]
  70.         public string Password { get; set; }
  71.     }
  72. }
Tags: C#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement