将ASP.NET Core部署到IIS并在浏览器中启动时,使用户无法工作 [英] ASP.NET Core get user not working when deployed to IIS and launch in browser

查看:455
本文介绍了将ASP.NET Core部署到IIS并在浏览器中启动时,使用户无法工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是重复的问题(据我研究).我的问题是,当我在VS 2019中运行ASP.NET Core 2.1 MVC应用程序时,一切工作正常,但是当我部署到本地IIS并在浏览器中启动应用程序时,它无法正常工作.我的操作系统是Windows 10.

This is not a duplicate question ( as far as I researched ). My problem is everything works nice when I run ASP.NET Core 2.1 MVC application in VS 2019 but when I deploy to local IIS and launch my application in browser it is not working. My operating system is Windows 10.

我在IIS中启用了以下功能

我正在做以下事情,以获取在浏览器中启动我的ASP.NET Core 2.1 MVC应用程序的人的用户名.

I am doing below to get username of person who launched my ASP.NET Core 2.1 MVC application in browser.

属性-> launchSettings.json

"windowsAuthentication": true,
 "anonymousAuthentication": false,

在Startup.cs中

public void ConfigureServices(IServiceCollection services)
{
   services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}

在Helper.cs中

public static string GetUserName(IHttpContextAccessor httpContextAccessor)
{
  string userName = string.Empty;
  if(httpContextAccessor != null &&
     httpContextAccessor.HttpContext != null &&
     httpContextAccessor.HttpContext.User != null &&
     httpContextAccessor.HttpContext.User.Identity != null)
    {
        userName = httpContextAccessor.HttpContext.User.Identity.Name;
        string[] usernames = userName.Split('\\');
        userName = usernames[1].ToUpper();
    }
        return userName;
}

在MyController.cs中

public class MyController : Controller
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public MyController(IHttpContextAccessor httpContextAccessor)
    {
      _httpContextAccessor = httpContextAccessor;            
    }

    [HttpPost]
    public JsonResult CreateOrder([FromBody] OrderModel model)
    {
      string username = Helper.GetUserName(_httpContextAccessor);
      .....
    }
}

当我在VS 2019中运行应用程序时,一切都很好,但是当我将应用程序发布到IIS并在浏览器中启动应用程序时,由于 httpContextAccessor.HttpContext.User.Identity.Name null而导致错误

When I run my application in VS 2019 everything is fine but when I publish my application to IIS and launch my application in browser I get error due to httpContextAccessor.HttpContext.User.Identity.Name null.

我还尝试了使用Claims之类的FindFirst之类的各种方法,但是当我在VS 2019中运行但未将我的应用程序发布到IIS并在浏览器中启动我的应用程序时,所有这些方法都很好用.我在做什么错.

I also tried various ways like FindFirst using Claims but all those work good when I run in VS 2019 but not publish my application to IIS and launch my application in browser. What mistake I am doing.

谢谢.

推荐答案

我找到了解决方案.

我在IIS中做了以下操作.双击身份验证.

I did below in my IIS. Double click on Authentication.

属性-> launchSettings.json web.config 无法解决该问题.

在Starup.cs中,

public void ConfigureServices(IServiceCollection services)
{
  services.AddHttpContextAccessor();
}

在Helper.cs中

public static string GetUserName(IHttpContextAccessor httpContextAccessor)
{
   string userName = string.Empty;
   if(httpContextAccessor != null &&
      httpContextAccessor.HttpContext != null &&
      httpContextAccessor.HttpContext.User != null &&
      httpContextAccessor.HttpContext.User.Identity != null)
      {
        userName = httpContextAccessor.HttpContext.User.Identity.Name;
        string[] usernames = userName.Split('\\');
        userName = usernames[1].ToUpper();
      }
      return userName;
}

在MyController.cs 中-我在操作方法中添加了Authorize标签.

In MyController.cs - I added Authorize tag to my action method.

public class MyController : Controller
{
    private readonly IHttpContextAccessor _httpContextAccessor;

    public MyController(IHttpContextAccessor httpContextAccessor)
    {
      _httpContextAccessor = httpContextAccessor;            
    }

    [HttpPost]
    [Authorize]
    public JsonResult CreateOrder([FromBody] OrderModel model)
    {
      string username = Helper.GetUserName(_httpContextAccessor);
      .....
    }
}

现在,当我在VS 2019中进行调试时以及在发布到IIS和其他人在浏览器中调用我的应用程序后,它都能很好地运行.

Now it works good when I debug in VS 2019 and after I publish to IIS and others calling my application in browser.

这篇关于将ASP.NET Core部署到IIS并在浏览器中启动时,使用户无法工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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