Asp.Net Core 2.1 Windows身份验证. HttpContext.User.Identity.Name在IIS中不起作用 [英] Asp.Net Core 2.1 Windows Auth. HttpContext.User.Identity.Name not working in IIS

查看:325
本文介绍了Asp.Net Core 2.1 Windows身份验证. HttpContext.User.Identity.Name在IIS中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:

Windows 10 带有IIS Express的IIS 10/Visual Studio 2017社区 已安装Windows身份验证安全功能 Windows身份验证已启用&基本身份验证已启用匿名身份验证已禁用

Windows 10 IIS 10 / Visual Studio 2017 Community w/ IIS Express Windows Authentication security feature installed Windows Authentication Enabled & Basic Authentication Enabled & Anonymous Authentication Disabled

我有一个Asp.Net Core 2.1项目,该项目将在Intranet中工作.所以我需要Windows身份验证.

I have a Asp.Net Core 2.1 Project and this project will work in intranet. So i need to windows authentication.

启动:

 public class Startup
    {
        private IConfigurationRoot _appSettings;
        public Startup(IHostingEnvironment env, IConfiguration configuration)
        {
            Configuration = configuration;
            _appSettings = new ConfigurationBuilder()
             .SetBasePath(env.ContentRootPath)
             .AddJsonFile("appsettings.json")
             .Build();

        }

        private 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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

            services.AddSession(options => {
                options.IdleTimeout = TimeSpan.FromMinutes(8640); //Sessions Time
            });
            services.AddDistributedMemoryCache();

            services.AddDbContext<PMContext>();
            services.AddSingleton<IConfigurationRoot>(_appSettings);
            PMContext.ConnectionString = _appSettings.GetConnectionString("DefaultConnection");
            services.AddAuthentication(IISDefaults.AuthenticationScheme);

        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }
            app.UseHangfireServer();
            app.UseHttpsRedirection();
            app.UseSession();
            app.UseStaticFiles();
            app.UseStatusCodePages();
            app.UseAuthentication();
            app.UseCookiePolicy();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Intro}/{action=Index}/{id?}");
            });

        }
    }
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseAuthentication();
    app.Run(async (context) =>
    {
        await context.Response.WriteAsync(JsonConvert.SerializeObject(new
        {                
            UserName = context?.User?.Identity?.Name
        }));
    });     

//在我的家庭控制器中;

//In my home controller;

var UserComputerName = contextAccessor.HttpContext.User.Identity.Name;

此代码可在我的本地主机上运行,​​但不能在IIS Server中运行:/我尝试了相同的错误解决方法,但无法解决.

This code working my localhost but don't working in IIS Server :/ I tried same error solves but I couldn't solve it.

推荐答案

确保已在IIS中禁用了匿名身份验证. 接下来,如果您调用User.Identity.Name,则将在控制器中获得用户详细信息,如果您使用Windows身份验证,则无需注入HttpContextAccessor.

Make sure you have disabled anonymous authentication in IIS. Next you will get the user details in the controller if you call User.Identity.Name , you don't need to inject HttpContextAccessor if your using windows auth.

这篇关于Asp.Net Core 2.1 Windows身份验证. HttpContext.User.Identity.Name在IIS中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆