I am trying to log in to my dashboard/page. When I log in it works perfectly fine it returns a token but when I Deserialze the token I get the error below:
when Im trying to Deserialize my Web Token on this line:
var returnedUser = JsonConvert.DeserializeObject(responseBody);
I get the error below:
Unexpected character encountered while parsing value: s. Path '', line 0, position 0
I am Using Blazor Server App and I'm not really sure where I'm getting it all wrong.
My code is below as well.
@page "/pages/authentication/login"
@layout LoginLayout
@inject HttpClient Http
@inject Blazored.SessionStorage.ISessionStorageService sessionStorage
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager NavigationManager
@using MileApp.Models
@using MileApp.Utilities
@using Newtonsoft.Json
<MudText Typo="Typo.h4" GutterBottom="true" >Sign In</MudText>
<MudText>Don't have an account? <MudLink Href="/pages/authentication/register">Sign Up</MudLink></MudText>
<MudTextField T="string" @bind-Value="@username" Label="E-mail" Variant="Variant.Outlined" Class="my-6"></MudTextField>
<MudTextField @bind-Value="@Password" Label="Password" Variant="Variant.Outlined" InputType="@PasswordInput" Adornment="Adornment.End" AdornmentIcon="@PasswordInputIcon" OnAdornmentClick="TogglePasswordVisibility" />
<div Class="d-flex justify-space-between align-center">
<MudCheckBox T="bool" Label="Remember me?" Color="Color.Primary" Class="ml-n1 my-3"></MudCheckBox>
<MudLink Href="/pages/authentication/forgot-password">Forgot pwd?</MudLink>
</div>
<MudButton Variant="Variant.Filled" Color="Color.Primary" Size="Size.Large" FullWidth="true" OnClick="@ValidateUser">Sign In</MudButton>
@code {
string username;
string Password;
bool PasswordVisibility;
InputType PasswordInput = InputType.Password;
string PasswordInputIcon = Icons.Material.Filled.VisibilityOff;
void TogglePasswordVisibility()
{
@if (PasswordVisibility)
{
PasswordVisibility = false;
PasswordInputIcon = Icons.Material.Filled.VisibilityOff;
PasswordInput = InputType.Password;
}
else
{
PasswordVisibility = true;
PasswordInputIcon = Icons.Material.Filled.Visibility;
PasswordInput = InputType.Text;
}
}
private async Task<bool> ValidateUser()
{
UserLogin u = new UserLogin();
u.username = username;
u.Password = Password;
var mydata = u;
string SerializedUser = JsonConvert.SerializeObject(u);
HttpRequestMessage httpRequestMessage = new HttpRequestMessage();
httpRequestMessage.Method = new HttpMethod("POST");
httpRequestMessage.RequestUri = new Uri("https://localhost:7101/api/Name/authenticate");
httpRequestMessage.Content = new StringContent(SerializedUser);
httpRequestMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
var response = await Http.SendAsync(httpRequestMessage);
var responseStatusCode = response.StatusCode;
var responseBody = await response.Content.ReadAsStringAsync();
//string test = $"{responseBody}";
if(responseStatusCode.ToString() == "OK")
{
try
{
var returnedUser = JsonConvert.DeserializeObject(responseBody);
}
catch(Exception hh)
{
string h = hh.Message;
}
((CustomAuthStateProvider)AuthenticationStateProvider).MarkUserAsAuthenticated(u.username);
NavigationManager.NavigateTo("/home/dashboard");
await sessionStorage.SetItemAsync("emailAdress", u.username);
return await Task.FromResult(true);
}
else
{
return await Task.FromResult(false);
}
}
}
namespace MileApp.Models
{
public class UserLogin
{
public string username { get; set; }
public string Password { get; set; }
}
}
{
//Web Token looks like this in the response Body when decoded I used JWT.io to decode
"unique_name": "Aubrey",
"nbf": 1645513472,
"exp": 1645515272,
"iat": 1645513472
}```
Unexpected character encountered while parsing value: s. Path '', line 0, position 0