Getting 401 Unauthorized for files in wwwroot on refresh.
I'm trying to solve this issue for a couple of days now, but I can't manage to do it, so I would really appreciate the help.
I've done a course for webapp in ASP.NET Core 2.1 WebAPI and Angular 6, using JWT Tokens as a form of authentication. I want to publish the app, but currently I came across a problem which instructor didn't have, I assume because he published first, then changed the project, meanwhile I looked updated version, so I did changes first and then tried to publish.
Anyway, thing is, when I run WebAPI and Angular server using "ng serve" everything works fine. I used "ng build", set a path to be "wwwroot" folder in API project and I got necessary files. After that I added these lines to Startup.cs:
app.UseDefaultFiles(); app.UseStaticFiles(); app.UseMvc(routes => { routes.MapSpaFallbackRoute( name: "spa-fallback", defaults: new { controller = "Fallback", action = "Index" } ); });
and added new file Fallback.cs:
public class Fallback : Controller { public IActionResult Index() { return PhysicalFile(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "index.html"), "text/HTML"); } }
When I run API, I get to homepage, after login, everything works fine. I can navigate to localhost:5000/members and get the members. The problem is when I press the refresh button for example on this members page. Doesn't matter where I am in the app, as soon as I refresh, the page becomes blank. I've checked requests and headers and noticed that it's always 401 Unauthorized, I guess because "WWW-Authenticate: Bearer " is without the JWT token itself.
Can someone tell me what am I missing and how to fix this?
When I navigate to localhost:5000/members after login, for the request I see 200 OK and URL http://localhost:5000/api/users?pageNumber=1&pageSize=5, meanwhile after I refresh, I see the URL is localhost:5000/members with 401 Unauthorized. Also, I tried sending request to localhost:5000/members in Postman, but adding Authorization header with "Bearer tokenhere" as well and I get this which are the files from wwwroot folder and 200 OK.
I assume Kestrel somehow doesn't understand how to navigate localhost:5000/members to Index action of Fallback controller, because when I use Postman, I hit breakpoint I set on Index action in Fallback.cs, hence why I get those files showed in the picture.
This is my Startup.cs.
0 comments:
Post a Comment