Adding Azure Active Directory authentication to a Blazor WebAssembly app - c#

I've got an existing Blazor Web Assembly app and I'm attempting to implement Azure AD authentication so that only users who have signed in with their work account can access the web app.
I'm attempting to follow this Microsoft guide. I've got the Microsoft.Authentication.WebAssembly.Msal nuget package installed however, in my Program.cs, I receive the following error:
IServiceCollection does not contain a definition for AddMsalAuthentication.
for the following block of code:
builder.Services.AddMsalAuthentication(options =>
{
builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
});
Any suggestions on why and how to resolve this?

In Visual Studio, created the Blazor WebAssembly App.
In Program.cs file, added the code which you have mentioned.
builder.Services.AddMsalAuthentication(options =>
{
builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
});
Initially even I got the same error.
In BlazorApp1.Server Application, Installed the Microsoft.Authentication.WebAssembly.Msal NuGet Package.
<PackageReference Include="Microsoft.Authentication.WebAssembly.Msal" Version="7.0.2" />
Now I am able to build the application without any issues.
My BlazorApp1.Server .csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>BlazorApp1.Server-****</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="7.0.0" />
<PackageReference Include="Microsoft.Authentication.WebAssembly.Msal" Version="7.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Client\BlazorApp1.Client.csproj" />
<ProjectReference Include="..\Shared\BlazorApp1.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="7.0.0" />
<PackageReference Include="Microsoft.AspNetCore.ApiAuthorization.IdentityServer" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.0" />
</ItemGroup>
</Project>
My Folder Structure:
As there are two Program.cs files (Client/Server), make sure you have installed the package in the application where you are adding the code.

Related

.NET 6 System.IO.FileNotFoundException: Could not load file or assembly System.Linq, Version=6.0.0.0

I am trying to migrate a project from .NET Core 3.1 to .NET 6.
I have followed the official migration guide from Microsoft.
During build in startup.Configure I get
System.IO.FileNotFoundException: 'Could not load file or assembly
'System.Linq, Version=6.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file
specified.'
This is .csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
<AssemblyName>Omicron.Insurance.Runner</AssemblyName>
<RootNamespace>Omicron.Insurance.Runner</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Omicron.Insurance.Entities" Version="1.0.139" />
<PackageReference Include="Omicron.Platform.Web.ApplicationInsights" Version="3.1.60" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.Azure.ServiceBus" Version="5.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="3.1.22" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="6.0.1" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
<PackageReference Include="NodaTime" Version="3.0.9" />
<PackageReference Include="ServiceBus.AttachmentPlugin" Version="6.2.0" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
I had to update AzureFunctionsVersion from v3 to v4. That solved the issue.
Upgrading Azure Functions wasn't mentioned in the official migration guide from Microsoft.
EDIT: you also need to upgrade the Azure Function App as well.
https://learn.microsoft.com/en-us/azure/azure-functions/functions-versions?tabs=in-process%2Cv4&pivots=programming-language-csharp#migrating-from-3x-to-4x
My case is different from the OP's but I want to share it for information. I run into this error when I try to use dnSpy to patch a C# DLL. It turns out the dnSpy compilation is targeting dependencies of my development machine. When I move the compiled DLL to the target machine, this error is raised as the dependencies are not met there. I have to compile it again on my target machine and the error is gone.

.NetCore 3.1API Could not load file or assembly 'Microsoft.AspNetCore.Hosting.Abstractions'

Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.AspNetCore.Hosting.Abstractions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
File name: 'Microsoft.AspNetCore.Hosting.Abstractions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
I am using aws to deploy my project to server(.NetCore 3.1). I am getting above error.But in local iis working fine.
I tried to install 'Microsoft.AspNetCore.Hosting.Abstractions'. But still I am facing same issue.
Please find below for package reference
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EPPlus" Version="5.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="3.1.5" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="4.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.6" />
<PackageReference Include="MongoDB.Driver" Version="2.10.3" />
<PackageReference Include="Nancy" Version="2.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.4.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Helpers\Helpers.csproj" />
<ProjectReference Include="..\Models\Models.csproj" />
<ProjectReference Include="..\Repository\Repository.csproj" />
<ProjectReference Include="..\Services\Services.csproj" />
</ItemGroup>
</Project>
I have gone through few link regarding same error. I tried all the method. But still i am getting same issue.
This could be similar to this issue (different file) https://learn.microsoft.com/en-us/answers/questions/8017/aspnet-core-310-site-wont-run-could-not-load-micro.html
could you please verify all the .Net core related packages in all the projects? Also, if you can post the package reference from all your projects, it will be easy to verify.
The issue could be due to one or more packages in any of your projects. Also, make sure the correct version of SDK is available in AWS. Try an empty app with the same project setup (same packages in all the 5 projects with lower package version ) and deploy this to AWS to find out the issue.

Error on System.MethodAccessException in 'Microsoft.Extensions.Logging.Configuration.LoggerProvider

I use visual studio to create the new web project which is MVC in .net Framework 4.8 under .net core 2.2.
When I use nuget to get the Microsoft.EntityFrameworkCore.SqlServer
and start to run (debug), I got the below exception
System.MethodAccessException: 'Attempt by method 'Microsoft.Extensions.Logging.Configuration.LoggerProviderConfigurationFactory.GetConfiguration(System.Type)'
to access method 'Microsoft.Extensions.Logging.ProviderAliasUtilities.GetAlias(System.Type)' failed.'
My Project properties is only this
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<DebugType>full</DebugType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.CookiePolicy" Version="2.2.8" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
</ItemGroup>
</Project>
Can you help to solve this problem ?
Thank you
the solution was downloading the Microsoft.Extensions packages and updating the versions so it can be equivalents.
here were the main answer and the discussions:
I suspect the issue is due to mismatched package versions. You will
certainly need to update all EntityFrameworkCore packages to the same
version. I expect you will also need to pull in the latest 3.1
versions of Microsoft.Extensions packages, since the EF packages
depend on the 3.1 versions of these packages.
discussion

.Net Core 2.0 Warning NU1603 during dotnet restore, problems with NuGet packages

(world!)
I am writing application APS .Net Core 2.0. In Test project after adding some NuGet packages to .csproj I meet some troubles. At the command dotnet restore an error pops up:
D:\01_Toci\100_GIT\Passenger\Passenger.Tests\Passenger.Tests.csproj :
warning NU1603: Castle.Core 4.0.0 depends on
System.ComponentModel.TypeConverter (>= 4.0.1) but
System.ComponentModel.TypeConverter 4.0.1 was not found. An
approximate best match of System.ComponentModel.TypeConverter 4.1.0
was resolved.
There is my .csproj code:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<AssemblyName>Passenger.Tests</AssemblyName>
<PackageId>Passenger.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
<RuntimeFrameworkVersion>1.1.1</RuntimeFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../Passenger.Core/Passenger.Core.csproj" />
<ProjectReference Include="../Passenger.Infrastructure/Passenger.Infrastructure.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="FluentAssertions" Version="4.19.0" />
<PackageReference Include="Moq" Version="4.7.8" />
<PackageReference Include="dotnet-test-nunit" Version="3.4.0-beta-3" />
</ItemGroup>
</Project>
and project.assert.json (it's too long, so i decide to use pastebin)
I tried to change versions in the json file,
"System.ComponentModel.TypeConverter": "4.1.0",
and many other options, but nothing has been done...I was stuck. I got mixed up with these nuggets and I do not know how to get out of it.
Please help me.
This worked out for me, just add System.ComponentModel.TypeConverter as the newest version.
Managed to reproduce the same error without the packed added.
Remember to use the netcoreapp2.0 framework if you say you are using 2.0 instead of 1.1 as you were using for your test project.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-*" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="System.ComponentModel.TypeConverter" Version="4.3.*" />
<PackageReference Include="FluentAssertions" Version="4.19.0" />
<PackageReference Include="Moq" Version="4.7.8" />
<PackageReference Include="dotnet-test-nunit" Version="3.4.0-*" />
</ItemGroup>
</Project>

.net-core-2.0 azure app service 502.5 error

Getting a 502.5 error after CI deployment to azure app service.
When running dotnet {myproject}.dll on the debug console this is the error I get:
Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.2.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors)
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at My.IOEducation.Api.Program.Main(String[] args) in D:\home\site\repository\My.IOEducation.Api\Program.cs:line 11
Running dotnet --version returns 2.0.0
Anyone else run into this yet and any suggestions on how to resolve?
UPDATE:
Here is the contents of the project file.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="Models\Report\" />
<Folder Include="wwwroot\" />
<Folder Include="DataAccess\ExternalApis\" />
<Folder Include="DataAccess\ExternalApis\Helpers\" />
<Folder Include="Models\Dashboard\" />
<Folder Include="Helpers\" />
<Folder Include="DataAccess\Redis\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.ResponseCompression" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="PowerBI.NetStandard.Api" Version="1.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="1.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUi" Version="1.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="1.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="1.0.0" />
<PackageReference Include="Dapper" Version="1.50.2" />
<PackageReference Include="StackExchange.Redis" Version="1.2.6" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<None Remove="DataAccess\.DS_Store" />
<None Remove=".DS_Store" />
</ItemGroup>
</Project>
I spoke to the aspnet IISIntegration team members and found my solution.
tldr: Empty out your wwwroot folder on kudu.
The issue relates to having old things leftover from previous 1.x deployments
Step 1:
Navigate to the Kudu Console (https://{yourapp}.scm.azurewebsites.net/)
Step 2:
Step 3:
(Note: navigate into the "site" directory)
(Note: there is a wwwroot folder within this wwwroot. You should delete the one that is in the "site" directory)
Step 4:
Add a new empty folder called wwwroot where you just deleted the previous one (within "site" directory)
(Note: my deployment failed when I didn't have the empty wwwroot folder in there)
Step 5: Redeploy your app and hopefully it works. Good luck
Found the issue. Let me start by adding a little more information. This was originally a .net-core-1.1 project that I updated to 2.0 following instructions provided by Microsoft. After upgrading, I had no issues at all running locally, but once I tried to publish my azure app service, i kept getting the IIS error. Last ditch effort was to create a new .net-core-2.0 project from scratch this morning and noticed that the new project file contained this:
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
</ItemGroup>
I added that to my existing project file (the one upgraded from 1.1) and now the error is gone and issue resolved.
Brand new .net core 2.1 app trying to deploy to new Azure app service with new app service plan. Same issues, namely 502.5 error ( HTTP Error 502.5 - Process Failure ).
I implemented the above steps and still ran into errors. I opted to use the chose the self-contained deployment mode (compared to framework dependent (win-x64)).
This resolved my issue.
This answer was also submitted to [question]: ASP.NET Core 2.0 Preview 2 on IIS error 502.5
Perhaps these questions should be linked.

Categories

Resources