命名空间“Microsoft.AspNetCore.Authentication”中不存在类型或命名空间名称“AzureAD”

Pra*_*tel 3 c# asp.net-core-mvc openid-connect .net-core visual-studio-2017

从以下链接https://azure.microsoft.com/en-gb/resources/samples/active-directory-aspnetcore-webapp-openidconnect-v2/关注示例项目后,我遇到了问题。它根本没有构建并抱怨以下 2 个错误,

i) 错误 CS0234:命名空间“Microsoft.AspNetCore.Authentication”中不存在类型或命名空间名称“AzureAD”(您是否缺少程序集引用?)

ii) 错误 CS0234:命名空间“Microsoft.AspNetCore”中不存在类型或命名空间名称“HttpsPolicy”(您是否缺少程序集引用?) 1>完成构建项目“WebApp-OpenIDConnect-DotNet.csproj”——失败.

我遵循了其他地方的一些建议,其中涉及在项目上运行“dotnet restore”,但这并没有奏效。

知道我需要在这里做什么吗?


有问题的文件如下。这是取自https://azure.microsoft.com/en-gb/resources/samples/active-directory-aspnetcore-webapp-openidconnect-v2/ 的“Startup.cs” 。

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?}");
            });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Pra*_*tel 6

所以结果证明文档已经过时,项目需要引用一些缺失的库,这最终很容易完成。为了解决这个问题,我做了以下事情,

i) 右键单击​​“依赖项”,然后单击“管理 NuGet 包...”。
ii) 单击“浏览”并搜索“Microsoft.AspNetCore.Authentication.AzureAD.UI”并安装此库。
iii) 单击“已安装”选项卡并更新现有库。

重建您的解决方案。还值得注意的是,“HomeController.cs”和“AccountController.cs”中的命名空间值应该相互匹配。例如,它们都应该是“命名空间 WebApp_OpenIDConnect_DotNet.Controllers”。与我的一个文件发生冲突,因为它引用了“命名空间 active_directory_aspnetcore_webapp_openidconnect_v2_master.Controllers”,这导致了编译错误。