Search code examples
azure-active-directoryblazor-webassembly.net-6.0

Blazor Webassembly Azure AD Authentication in .net 6.0 exception invalid Url


I upgrade my Client Project to .net 6.0, I got the property undefined error after deploying in IIS, I added the TrimmerRootAssembly to csproj


    <ItemGroup>
        <TrimmerRootAssembly Include="Microsoft.Authentication.WebAssembly.Msal" />
      </ItemGroup>

as Suggestion in this Question Blazor WASM Net 6 Preview 4 Azure AD - There was an error trying to log you in: 'Cannot read property 'toLowerCase' of undefined' but then I got another error invalid Url

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
  Unhandled exception rendering component: Failed to construct 'URL': Invalid URL
  TypeError: Failed to construct 'URL': Invalid URL
      at new u (https://devdms/_content/Microsoft.Authentication.WebAssembly.Msal/AuthenticationService.js:2:191011)
      at Function.init (https://devdms/_content/Microsoft.Authentication.WebAssembly.Msal/AuthenticationService.js:2:196273)
      at https://devdms/_framework/blazor.webassembly.js:1:3332
      at new Promise (<anonymous>)
      at Object.beginInvokeJSFromDotNet (https://devdms/_framework/blazor.webassembly.js:1:3306)
      at Object.Rt [as invokeJSFromDotNet] (https://devdms/_framework/blazor.webassembly.js:1:59738)
      at _mono_wasm_invoke_js_blazor (https://devdms/_framework/dotnet.6.0.1.716ng6fo9h.js:1:194546)
      at https://devdms/_framework/dotnet.wasm:wasm-function[219]:0x1a129
      at https://devdms/_framework/dotnet.wasm:wasm-function[167]:0xcaf7
      at https://devdms/_framework/dotnet.wasm:wasm-function[166]:0xba0a

Microsoft.JSInterop.JSException: Failed to construct 'URL': Invalid URL TypeError: Failed to construct 'URL': Invalid URL

any idea what the problem is? btw the project work locally without any problem


Solution

  • Please check the address formed in web and where is it going wrong and modify the authentication request in code.

    Open appsettings.json file and check all the values like authority ,callback path are configured correctly or instance ,domain ... are given correctly.

    "AzureAd": {
        "Instance": "https://login.microsoftonline.com/",
        "Domain": " ",
        "TenantId": " ",
        "ClientId": " ",
        "CallbackPath": "/signin-oidc"
      },
    

    and make sure valid redirect url is given in azure app registration under authentication blade Ex:https://localhost:1443/signin-oidc. If any scopes are added , make sure to grant consent to permissions.

    Remove bin & obj folders, clear the cache from browser. Delete all the files in destination folder on the server and then rebuild the solution before publishing .

    It can be issue with chrome extension too.

    Reference: Blazor WASM Net 6 Preview 4 Azure AD (github.com)