Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET Core在部署到IIS并在浏览器中启动时让用户无法工作_C#_Iis_Asp.net Core Mvc - Fatal编程技术网

C# ASP.NET Core在部署到IIS并在浏览器中启动时让用户无法工作

C# ASP.NET Core在部署到IIS并在浏览器中启动时让用户无法工作,c#,iis,asp.net-core-mvc,C#,Iis,Asp.net Core Mvc,就我所研究的而言,这不是一个重复的问题。我的问题是,当我在VS2019中运行ASP.NET Core 2.1 MVC应用程序时,一切都很好,但当我部署到本地IIS并在浏览器中启动应用程序时,一切都不起作用。我的操作系统是Windows 10 我已经在IIS中打开了下面的功能 我在下面做的是获取在浏览器中启动我的ASP.NET Core 2.1 MVC应用程序的用户的用户名 在Properties->launchSettings.json中 在Startup.cs中 在MyController.c

就我所研究的而言,这不是一个重复的问题。我的问题是,当我在VS2019中运行ASP.NET Core 2.1 MVC应用程序时,一切都很好,但当我部署到本地IIS并在浏览器中启动应用程序时,一切都不起作用。我的操作系统是Windows 10

我已经在IIS中打开了下面的功能

我在下面做的是获取在浏览器中启动我的ASP.NET Core 2.1 MVC应用程序的用户的用户名

在Properties->launchSettings.json中

在Startup.cs中

在MyController.cs中

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

我还尝试了各种方法,比如使用声明的FindFirst,但当我在VS2019中运行时,这些方法都很好,但没有将我的应用程序发布到IIS并在浏览器中启动我的应用程序。我犯了什么错误


提前感谢。

必须为IIS站点单独启用Windows身份验证。很可能,您还没有这样做,因此不会发生自动授权,因此用户名为空

也就是说,您不应该将IHttpContextAccessor注入控制器。HttpContext已经可以直接通过HttpContext访问。此外,您的助手在这里完全无用,因为您只是在替换:

HttpContext.User.Identity.Name
与:


实际上,您实际上可以只使用User.Identity.Name,因为默认情况下主体存储在控制器的User属性中。

必须为IIS站点单独启用Windows Auth。很可能,您还没有这样做,因此不会发生自动授权,因此用户名为空

也就是说,您不应该将IHttpContextAccessor注入控制器。HttpContext已经可以直接通过HttpContext访问。此外,您的助手在这里完全无用,因为您只是在替换:

HttpContext.User.Identity.Name
与:

事实上,您实际上可以只使用User.Identity.Name,因为主体默认存储在控制器的User属性中。

我找到了解决方案

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

Properties->launchSettings.json和web.config没有解决此问题

在Starup.cs中

在Helper.cs中

在MyController.cs中,我将Authorize标记添加到我的操作方法中

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);
      .....
    }
}
现在,当我在VS2019中调试时,以及在我发布到IIS和其他人在浏览器中调用我的应用程序后,它运行良好。

我找到了解决方案

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

Properties->launchSettings.json和web.config没有解决此问题

在Starup.cs中

在Helper.cs中

在MyController.cs中,我将Authorize标记添加到我的操作方法中

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);
      .....
    }
}

现在,当我在VS2019中调试时,以及在我发布到IIS和其他人在浏览器中调用我的应用程序后,它运行良好。

非常感谢@Ziggler的回答。我遇到的几乎所有研究都建议修改web.config和launchSettings.json,以及在IIS配置屏幕中启用Windows身份验证和禁用匿名身份验证,但这些都没有解决问题。以下内容确实解决了我的问题

将windows身份验证用户名拉出并在返回值之前将其清除

 public class Helper : IHelper{

    private readonly ILogger<Helper> _logger;
    private readonly IHttpContextAccessor _httpContextAccessor;

    public Helper(ILogger<Helper> logger, IHttpContextAccessor httpContextAccessor) { 
        _logger = logger;
        _httpContextAccessor = httpContextAccessor;
    }

    public int GetSSO()
    {
        try
        {
            //Remove DSM
            string temp = Regex.Replace(_httpContextAccessor.HttpContext.User.Identity.Name, @"[DSM]", "");
            //Remove special characters
            temp = Regex.Replace(temp, @"[^\w\.@-]", "");
            //Convert string to int
            int sso = Int32.Parse(temp);

            return sso;
        }
        catch (Exception e)
        {
            _logger.LogError("::::Error Getting SSO:::: " + e);
            return 000000000;
        }
    }

非常感谢@Ziggler的回答。我遇到的几乎所有研究都建议修改web.config和launchSettings.json,以及在IIS配置屏幕中启用Windows身份验证和禁用匿名身份验证,但这些都没有解决问题。以下内容确实解决了我的问题

将windows身份验证用户名拉出并在返回值之前将其清除

 public class Helper : IHelper{

    private readonly ILogger<Helper> _logger;
    private readonly IHttpContextAccessor _httpContextAccessor;

    public Helper(ILogger<Helper> logger, IHttpContextAccessor httpContextAccessor) { 
        _logger = logger;
        _httpContextAccessor = httpContextAccessor;
    }

    public int GetSSO()
    {
        try
        {
            //Remove DSM
            string temp = Regex.Replace(_httpContextAccessor.HttpContext.User.Identity.Name, @"[DSM]", "");
            //Remove special characters
            temp = Regex.Replace(temp, @"[^\w\.@-]", "");
            //Convert string to int
            int sso = Int32.Parse(temp);

            return sso;
        }
        catch (Exception e)
        {
            _logger.LogError("::::Error Getting SSO:::: " + e);
            return 000000000;
        }
    }

我需要助手,因为我需要在很多地方使用这个逻辑,我不想重复它。我有这么多控制器,控制器中几乎每个方法都需要用户名来记录。如果我不能注入IHttpContextAccessor,我的助手类将如何访问它?如果在我的问题中看到我的IIS图片,我检查了Windows身份验证。还有什么我需要做的吗。我在看HttpContext,没有逻辑。这只是User.Identity.Name。我得到了null,我发布了我在问题中所做的事情。我将GetUserName从Helper.cs移动到MyController.cs,但它不起作用。我需要Helper,因为我需要在很多地方使用此逻辑,我不想重复它。我有这么多控制器,控制器中几乎每个方法都需要用户名来记录。如果我不能注入IHttpContextAccessor,我的助手类将如何访问它?如果在我的问题中看到我的IIS图片,我检查了Windows身份验证。还有什么我需要做的吗。我在看HttpContext,没有逻辑。这只是User.Identity.Name。我得到了null,我发布了我在问题中所做的事情。我将GetUserName从Helper.cs移动到MyController.cs,但它不起作用。如果
找到解决方案后,发布自己的答案并接受。@LexLi。。我发布了我的解决方案。我将在2天后接受。为什么要对此进行否决投票。除非我在下面的答案中执行步骤1和2,否则可能会重复IIS。身份验证无效。所有这些帖子都没有提到这一点。我很好。我把它修好了,我相信他们会有很多人会有这个问题,这篇文章会有所帮助。如果你找到了解决办法,发表你自己的答案并接受它。@LexLi。。我发布了我的解决方案。我将在2天后接受。为什么要对此进行否决投票。除非我在下面的答案中执行步骤1和2,否则可能会重复IIS。身份验证无效。所有这些帖子都没有提到这一点。我很好。我把它修好了,我相信他们会有很多人会有这个问题,这篇文章会有所帮助。你到底是怎么修好的?我的用户名为空。请参阅上面我的解决方案帖子。它有图像。我就是这么做的…你到底是怎么修好的?我的用户名为空。请参阅上面我的解决方案帖子。它有图像。我就是这么做的。。。
public void ConfigureServices(IServiceCollection services)
{
  services.AddHttpContextAccessor();
}
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;
}
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);
      .....
    }
}
 public class Helper : IHelper{

    private readonly ILogger<Helper> _logger;
    private readonly IHttpContextAccessor _httpContextAccessor;

    public Helper(ILogger<Helper> logger, IHttpContextAccessor httpContextAccessor) { 
        _logger = logger;
        _httpContextAccessor = httpContextAccessor;
    }

    public int GetSSO()
    {
        try
        {
            //Remove DSM
            string temp = Regex.Replace(_httpContextAccessor.HttpContext.User.Identity.Name, @"[DSM]", "");
            //Remove special characters
            temp = Regex.Replace(temp, @"[^\w\.@-]", "");
            //Convert string to int
            int sso = Int32.Parse(temp);

            return sso;
        }
        catch (Exception e)
        {
            _logger.LogError("::::Error Getting SSO:::: " + e);
            return 000000000;
        }
    }