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

时间:2018-08-06 15:22:35

标签: 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/跟踪示例项目后,我遇到了问题。 它根本无法构建,并且抱怨以下两个错误,

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

ii)错误CS0234:类型或名称空间名称'HttpsPolicy'在名称空间'Microsoft.AspNetCore'中不存在(您是否缺少程序集引用?) 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?}");
            });
        }
    }
}

2 个答案:

答案 0 :(得分:2)

因此,事实证明该文档已过时,并且该项目需要引用一些缺少的库,最后很容易做到。要解决此问题,我做了以下操作,

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”,因此发生了冲突。

答案 1 :(得分:0)

使用nuget安装以下软件包:

install-package Microsoft.AspNetCore.DataProtection install-package Microsoft.AspNetCore.Antiforgery install-package Microsoft.AspNetCore.Authentication.Core install-package Microsoft.AspNetCore.Authentication.Cookies install-package Microsoft.AspNetCore.Authentication.JwtBearer install-package Microsoft.AspNetCore.Authentication.OAuth install-package Microsoft.AspNetCore.Authentication.OpenIdConnect install-package Microsoft.AspNetCore.Authentication install-package Microsoft.AspNetCore.Authorization.Policy install-package Microsoft.AspNetCore.Authorization install-package Microsoft.AspNetCore.Cors install-package Microsoft.AspNetCore.Diagnostics.Abstractions install-package Microsoft.AspNetCore.Html.Abstractions install-package Microsoft.AspNetCore.JsonPatch install-package Microsoft.AspNetCore.Localization install-package Microsoft.Extensions.Localization.Abstractions install-package Microsoft.AspNetCore.Localization install-package Microsoft.AspNetCore.Mvc.Abstractions install-package Microsoft.AspNetCore.Routing.Abstractions install-package Microsoft.AspNetCore.Mvc.Abstractions install-package Microsoft.AspNetCore.Mvc.Analyzers install-package Microsoft.AspNetCore.ResponseCaching.Abstractions install-package Microsoft.AspNetCore.Routing install-package Microsoft.Extensions.DependencyInjection install-package Microsoft.AspNetCore.Mvc.Core install-package Microsoft.AspNetCore.Mvc.ApiExplorer install-package Microsoft.AspNetCore.Mvc.Cors install-package Microsoft.Extensions.Localization install-package Microsoft.AspNetCore.Mvc.DataAnnotations install-package Microsoft.AspNetCore.Mvc.Formatters.Json install-package Microsoft.AspNetCore.Razor.Language install-package Microsoft.CodeAnalysis.Razor install-package Microsoft.AspNetCore.Mvc.Razor.Extensions install-package Microsoft.AspNetCore.Mvc.ViewFeatures install-package Microsoft.AspNetCore.Razor install-package Microsoft.AspNetCore.Razor.Runtime install-package Microsoft.Extensions.Caching.Abstractions install-package Microsoft.Extensions.Caching.Memory install-package Microsoft.Extensions.FileProviders.Composite install-package Microsoft.AspNetCore.Mvc.Razor install-package Microsoft.AspNetCore.Mvc.Localization install-package Microsoft.AspNetCore.Mvc.RazorPages install-package Microsoft.AspNetCore.Mvc.TagHelpers install-package Microsoft.AspNetCore.Razor.Design install-package Microsoft.AspNetCore.Mvc install-package Microsoft.AspNetCore.Authentication.AzureAD.UI

以及可选的: install-package Microsoft.NET.Sdk.Razor