Skip to content

Commit

Permalink
fixes JWT authentication with .NET 8
Browse files Browse the repository at this point in the history
  • Loading branch information
rumen-yankov committed Nov 23, 2023
1 parent e19cb6c commit 8afe254
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 27 deletions.
43 changes: 24 additions & 19 deletions WebVella.Erp.Plugins.Project/Controllers/ProjectController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,34 +480,39 @@ public ContentResult TimeTrackJs([FromQuery]string file = "")
}
}


[AllowAnonymous]

[Route("api/v3.0/p/project/user/get-current")]
[HttpGet]
public ActionResult GetCurrentUser()
{
var response = new ResponseModel();

try
{
var boz = CurrentUserId;

var user = SecurityContext.CurrentUser;
response.Success = true;
response.Message = "Tested";
return Json(response);
}
catch (Exception ex)
{
response.Success = false;
response.Message = ex.Message;
return Json(response);
}
// var response = new ResponseModel();

// try
// {
//var boz = CurrentUserId;

// var user = SecurityContext.CurrentUser;
// response.Success = true;
// response.Message = "Tested";
// return Json(response);
// }
// catch (Exception ex)
// {
// response.Success = false;
// response.Message = ex.Message;
// return Json(response);
// }
return Json( new WvUser { Email = "email", Id = CurrentUserId.Value });
}

#endregion

}

public class WvUser
{
public Guid Id { get; set; }
public string Email { get; set; }
}

}
6 changes: 4 additions & 2 deletions WebVella.Erp.Site.Project/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
});
app.UseStaticFiles(); //Workaround for blazor to work - https://github.com/dotnet/aspnetcore/issues/9588
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();


app
.UseErpPlugin<NextPlugin>()
Expand All @@ -171,6 +170,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
.UseErpMiddleware()
.UseJwtMiddleware();

app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
Expand Down
8 changes: 5 additions & 3 deletions WebVella.Erp.Site/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,18 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
});
app.UseStaticFiles(); //Workaround for blazor to work - https://github.com/dotnet/aspnetcore/issues/9588
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();


app
.UseErpPlugin<SdkPlugin>()
.UseErp()
.UseErpMiddleware()
.UseJwtMiddleware();

app.UseEndpoints(endpoints =>
app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
Expand Down
3 changes: 3 additions & 0 deletions WebVella.Erp.Web/Middleware/JwtMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public async Task Invoke(HttpContext context)
{
var user = new SecurityManager().GetUser(new Guid(nameIdentifier));
context.Items["User"] = user;

var identity = new ClaimsIdentity(jwtToken.Claims, "jwt");
context.User = new ClaimsPrincipal(identity);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public async ValueTask<WvUser> GetCurrentUserAsync()
{
var httpClient = await GetAuthorizedHttpClientAsync();

//return await httpClient.GetAndReadAsJsonAsync<WvUser>($"{apiProjectRoot}user/get-current");
return await httpClient.PostAndReadAsJsonAsync<object,WvUser>($"/api/v3/en_US/eql",null);
return await httpClient.GetAndReadAsJsonAsync<WvUser>($"{apiProjectRoot}user/get-current");
//return await httpClient.PostAndReadAsJsonAsync<object,WvUser>($"/api/v3/en_US/eql",null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task _loginBtnClick()
if (!result)
throw new ValidationException("Грешно потребителско име или парола");

var user = await ApiService.GetCurrentUserAsync();
// var user = await ApiService.GetCurrentUserAsync();

if (!String.IsNullOrWhiteSpace(_returnUrl))
Navigator.NavigateTo(_returnUrl);
Expand Down

0 comments on commit 8afe254

Please sign in to comment.