0

I have a Blazor WebAssembly app which is server hosted.

When I deploy it on an internal work network, the address is just the IP address, e.g. http://192.168.1.23. That works, but it's not so user friendly. I want it to be http://192.168.1.23/myapp.

I've done some reading on this here (Microsoft), and also here (Stackoverflow). They suggest a couple of things:

  1. In Client side code, index.html, change <base href="/" /> to <base href="/myapp/" />.

  2. In Server side code, Startup.cs, add app.UsePathBase("player"); early on in the Configure() method.

  3. Microsoft also says how to edit the launch settings so that debug still works normally, and they include adding the following to the launchSettings.json file:

    "commandLineArgs": "--pathbase=/myapp", "launchUrl": "myapp"

They don't say if this is in the Client or Server launchSettings.json file, and I can't get it to work anyway. Even if I add "launchUrl": "https://www.google.com" everywhere, nothing different happens when I run it.

Ignoring number 3 above though, the app will start, and I can get to the correct place by manually adding /myapp to the address. So far so good.

I am using cookie authentication though, and when I try to retrieve the user details from the user details from the HttpContext using this method on a hub:

public static User GetAuthenticatedUser(this HttpContext context)
        {
            var idClaim = context.User.Claims.FirstOrDefault(c => c.Type.Equals(ClaimTypes.NameIdentifier));
            var nameClaim = context.User.Claims.FirstOrDefault(c => c.Type.Equals(ClaimTypes.Name));

            return new User
            {
                Id = int.Parse(idClaim.Value),
                Name = nameClaim.Value
            };
        }

It fails, with each claim being null. It did not before I set the base path. How can I overcome this issue?

Note: It works on a regular controller, it's specifically a hub it is failing on, so it's as if the hub is not receiving the same HttpContext as before. The hub was also setup in Startup.cs, with the following in the Configure() method:

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHub<MyHub>("/myhub1");
            endpoints.MapRazorPages();
            endpoints.MapControllers();
            endpoints.MapFallbackToFile("index.html");
        });

Any advice is much appreciated.

Greg
  • 1,673
  • 4
  • 20
  • 27

0 Answers0