I have an issue after following an example project from the following link https://azure.microsoft.com/en-gb/resources/samples/active-directory-aspnetcore-webapp-openidconnect-v2/. It doesn't build at all and complains of the following 2 errors,
i) error CS0234: The type or namespace name 'AzureAD' does not exist in the namespace 'Microsoft.AspNetCore.Authentication' (are you missing an assembly reference?)
ii) error CS0234: The type or namespace name 'HttpsPolicy' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?) 1>Done building project "WebApp-OpenIDConnect-DotNet.csproj" -- FAILED.
I have followed some suggestions from elsewhere, which involved running "dotnet restore" on the project, but this hasn't worked.
Have any idea on what I need to do here?
File in question is below. This is "Startup.cs" taken from https://azure.microsoft.com/en-gb/resources/samples/active-directory-aspnetcore-webapp-openidconnect-v2/.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.AzureAD.UI;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace active_directory_aspnetcore_webapp_openidconnect_v2_master
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
services.AddMvc(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
So it turned out the documentation was outdated and that the project required references to some of the missing libraries, which was easily done in the end. To resolve the issue I did the following,
i) Right click on "Dependencies" and click on "Manage NuGet packages...".
ii) Click on "Browse" and search for "Microsoft.AspNetCore.Authentication.AzureAD.UI" and install this library.
iii) Click on the "Installed" tab and update the existing libraries.
Rebuild your solution. It is also worth noting that the namespace value in both "HomeController.cs" and "AccountController.cs" should match each other. So for example they both should be "namespace WebApp_OpenIDConnect_DotNet.Controllers". There was a conflict with one of my files as it referenced "namespace active_directory_aspnetcore_webapp_openidconnect_v2_master.Controllers", which caused a compile error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With