Reputation: 1328
I published my project on the host . My project works on the local system without any problems.
But when publishing on the hosts, it gives an error. The version of IIS in our host is 10.
And my webconfig file content is as follow:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\LosacoWeb.Server.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>
I attached picture of error as follow:
And the error message:
Uncaught TypeError: Cannot read properties of undefined (reading 'register')
at (index):17
/favicon.ico:1 Failed to load resource: the server responded with a status of 404 (Not Found)
dotnet.5.0.4.js:1 mono_wasm_runtime_ready fe00e07a-5519-4dfe-b35a-f867dbaf2e28
blazor.webassembly.js:1 System.Reflection.TargetInvocationException: Arg_TargetInvocationException
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1 ---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.AspNetCore.Components.Authorization, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies.
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1 IO_FileName_Name, Microsoft.AspNetCore.Components.Authorization, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1 at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[<Main>d__0](<Main>d__0& stateMachine)
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1 at LosacoWeb.Client.Program.Main(String[] args)
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1 at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1 Exception_EndOfInnerExceptionStack
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1 at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1 at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
d.printErr @ blazor.webassembly.js:1
blazor.webassembly.js:1 at Microsoft.AspNetCore.Components.WebAssembly.Hosting.EntrypointInvoker.InvokeEntrypoint(String assemblyName, String[] args)
After some times checking our code I known that our problem is in JWTAuthenticationStateProvider class. when we published our website without that class Our website works fine. I add that class code as follow:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text.Json;
using System.Threading.Tasks;
using LosacoWeb.Client.Helpers;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.JSInterop;
namespace LosacoWeb.Client.Auth
{
public class JWTAuthenticationStateProvider : AuthenticationStateProvider, ILoginService
{
private readonly IJSRuntime js;
private readonly string TokenKey = "******";
private readonly HttpClient httpClient;
private AuthenticationState Anonymous =>
new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
public JWTAuthenticationStateProvider(IJSRuntime js, HttpClient httpClient)
{
this.js = js;
this.httpClient = httpClient;
}
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var token = await js.GetFromLocalStorage(TokenKey);
if (string.IsNullOrEmpty(token))
{
return Anonymous;
}
return BuildAuthenticationState(token);
}
public AuthenticationState BuildAuthenticationState(string token)
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token);
return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(ParseClaimsFromJwt(token), "jwt")));
}
private IEnumerable<Claim> ParseClaimsFromJwt(string jwt)
{
var claims = new List<Claim>();
var payload = jwt.Split(".")[1];
var jsonBytes = ParsBase64WithoutPadding(payload);
var keyValuePairs = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonBytes);
keyValuePairs.TryGetValue(ClaimTypes.Role, out object roles);
if (roles != null)
{
if (roles.ToString().Trim().StartsWith("["))
{
var parsedRoles = JsonSerializer.Deserialize<string[]>(roles.ToString());
foreach (var parsedRole in parsedRoles)
{
claims.Add(new Claim(ClaimTypes.Role, parsedRole));
}
}
else
{
claims.Add(new Claim(ClaimTypes.Role, roles.ToString()));
}
keyValuePairs.Remove(ClaimTypes.Role);
}
claims.AddRange(keyValuePairs.Select(kvp => new Claim(kvp.Key, kvp.Value.ToString())));
return claims;
}
private byte[] ParsBase64WithoutPadding(string base64)
{
switch (base64.Length % 4)
{
case 2: base64 += "=="; break;
case 3: base64 += "="; break;
}
return Convert.FromBase64String(base64);
}
public async Task Login(string token)
{
await js.SetInLocalStorage(TokenKey, token);
var authState = BuildAuthenticationState(token);
NotifyAuthenticationStateChanged(Task.FromResult(authState));
}
public async Task LogOut()
{
await js.RemoveItem(TokenKey);
httpClient.DefaultRequestHeaders.Authorization = null;
NotifyAuthenticationStateChanged(Task.FromResult(Anonymous));
}
}
}
In program.cs:
builder.Services.AddScoped<JWTAuthenticationStateProvider>();
builder.Services.AddScoped<AuthenticationStateProvider, JWTAuthenticationStateProvider>
(provider => provider.GetRequiredService<JWTAuthenticationStateProvider>());
builder.Services.AddScoped<ILoginService, JWTAuthenticationStateProvider>
(provider => provider.GetRequiredService<JWTAuthenticationStateProvider>());
Upvotes: 1
Views: 1735
Reputation: 36
just copy all of scripts in script folder in shared folder into wwwroot folder.
When running WebSite in frameworks higher than 5 in BLAZOR, all the script files used in the mentioned folder must be copied. Because the physical path defined by default in IIS
does not have direct access to these folders in the shared layer.
best regards.
Upvotes: 2