c# - 命名空间 'AzureAD' 中不存在类型或命名空间名称 'Microsoft.AspNetCore.Authentication'

标签 c# .net-core visual-studio-2017 asp.net-core-mvc openid-connect

在遵循以下链接中的示例项目后我遇到了问题 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?}");
            });
        }
    }
}

最佳答案

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

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”,这导致了编译错误。

关于c# - 命名空间 'AzureAD' 中不存在类型或命名空间名称 'Microsoft.AspNetCore.Authentication',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51710925/

相关文章:

c# - 如何在c#中使用varchar数据类型?

c# - 如何访问剪贴板图像并将其保存在 ASP.NET Web 应用程序中的服务器位置?

c# - 健康检查端点生成的 HealthReport 对象结构卡住 Swagger 文档页面

.net-core - 过滤 dotnetcore 运行状况检查日志

c++ - "The C Compiler [...] is not able to compile a simple test program"使用 CMake 和 Android NDK

c# - 将对象序列化为 XML 时出现意外结果

c# - 如何在代码隐藏的 asp.net 中使标签闪烁

docker - 在 Raspberry Pi 3B+ 上运行 ASP .NET Core

c++ - 为什么使用__LINE__的此代码在MSVC下以 Release模式而不是在 Debug模式下进行编译?

javascript - Visual Studio 2017 中用于 JavaScript 和 TypeScript 文件的 Visual Studio Code 颜色主题