在命名空间'Microsoft.AspNetCore.Authentication'中,类型或命名空间名称'AzureAD'不存在。

4
在按照以下链接的示例项目后,我遇到了问题: https://azure.microsoft.com/en-gb/resources/samples/active-directory-aspnetcore-webapp-openidconnect-v2/。它根本无法构建,并抱怨以下两个错误:
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.
我已经尝试了一些其他地方的建议,其中包括在项目上运行“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?}");
            });
        }
    }
}

OpenIdConnect 有一些更改。需要更改您的代码。 - Md. Abdul Alim
我该如何找出需要进行哪些更改?您是否有任何来源/信息可以提供? - Pradeep Patel
你能分享你的代码吗? - Md. Abdul Alim
是的,我已经更新了上面的查询,包括文件中的源。 - Pradeep Patel
这不是我遇到的同样问题。你的问题是另一个问题。你需要从Microsoft Azure获取租户ID。https://github.com/Azure/azure-xplat-cli/issues/1948 - Md. Abdul Alim
1个回答

7
因为文档已经过时,所以项目需要引用一些缺失的库,在最后轻松完成了。为解决这个问题,我做了以下几步:
i)右键单击“Dependencies”,然后单击“Manage NuGet packages...”。 ii)单击“Browse”,搜索“Microsoft.AspNetCore.Authentication.AzureAD.UI”,并安装这个库。 iii)单击“Installed”选项卡并更新现有库。
重新构建您的解决方案。值得注意的是,“HomeController.cs”和“AccountController.cs”中的命名空间值应该相互匹配。例如,它们都应该是“namespace WebApp_OpenIDConnect_DotNet.Controllers”。我的一个文件与之冲突,因为它引用了“namespace active_directory_aspnetcore_webapp_openidconnect_v2_master.Controllers”,这导致编译错误。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接