LoginPage

LoginPage

LoginPage

Authentication and Authorization Isuue in JWT ASP.NET

22 Views Asked by At

@page "/login"
@rendermode InteractiveServer

<div class="container">
    <div class="row">
        <div class="col-sm-4">
            <h2>LoginPage</h2>
            <EditForm Model="@Login" OnValidSubmit="LoginClicked" FormName="loginForm">
                <DataAnnotationsValidator />
                <div class="mb-3">
                    <label for="exampleInputEmail1" class="form-label">Email address</label>
                    <InputText @bind-Value="Login.Email" class="form-control"></InputText>
                    <div id="emailHelp" class="form-text">We'll never share your email with anyone else.</div>
                </div>
                <div class="mb-3">
                    <label for="exampleInputPassword1" class="form-label">Password</label>
                    <InputText @bind-Value="Login.Password" class="form-control"></InputText>
                </div>
                <button type="submit" class="btn btn-primary">Login</button>
            </EditForm>
        </div>
    </div>
</div>

@code {
    public LoginDTO Login = new();
    async Task LoginClicked()
    {
        LoginResopnse resopnse = await accountService.LoginAsync(Login);
        if (!resopnse.Flag)
        {
            await js.InvokeVoidAsync("alert", resopnse.Message);
            return;
        }

        CustomAuthenticationStateProvider.UpdateAuthenticationState(resopnse.JWTToken);
        NavManager.NavigateTo("/", forceLoad: true);
    }
}

 public static void UpdateAuthenticationState(string jwtToken)
 {
     var claimPrincipal = new ClaimsPrincipal();
     if (!string.IsNullOrEmpty(jwtToken))
     {
         Constants.JWTToken = jwtToken;
         var getUserClaim = DecryptToken(jwtToken);
         claimPrincipal = SetClaimPrincipal(getUserClaim);
     }
     else
     {
         Constants.JWTToken = null;
     }
     CustomAuthenticationStateProvider _stateProvider = new CustomAuthenticationStateProvider();
     _stateProvider.NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(claimPrincipal)));

 }


 <AuthorizeView>
     <Authorized>
         <div class="nav-item px-3">
             <NavLink class="nav-link" href="weather">
                 <span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Wheather
             </NavLink>
         </div>
     </Authorized>
 </AuthorizeView>

I have tried the above format and i want that after succesfull login , The Authorised user should able to view those things what are mentioned inside the AuthorisezeView . I think there have some issue in the NotifyAuthenticationChanged Funnction implementation if some one have any idea share .

0

There are 0 best solutions below