ASP.NET Core get user not working when deployed to IIS and launch in browser
这不是重复的问题(据我研究)。我的问题是,当我在VS 2019中运行ASP.NET Core 2.1 MVC应用程序时,一切工作正常,但是当我部署到本地IIS并在浏览器中启动应用程序时,它无法正常工作。我的操作系统是Windows10。
我下面在IIS中已打开
我在下面做的工作是获取在浏览器中启动我的ASP.NET Core 2.1 MVC应用程序的用户的用户名。
在Properties-> launchSettings.json
中
1 2 | "windowsAuthentication": true, "anonymousAuthentication": false, |
在Startup.cs
中
1 2 3 4 | public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); } |
在Helper.cs
中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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
中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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而出现错误。
我还尝试了使用Claims之类的FindFirst之类的各种方法,但是当我在VS 2019中运行但未将我的应用程序发布到IIS并在浏览器中启动我的应用程序时,所有这些方法都很好用。我在做什么错。
预先感谢。
我找到了解决方案。
我在IIS中做了以下操作。双击身份验证。
属性-> launchSettings.json和web.config无法解决该问题。
在Starup.cs中,
1 2 3 4 | public void ConfigureServices(IServiceCollection services) { services.AddHttpContextAccessor(); } |
在Helper.cs
中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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标签添加到了我的操作方法中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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和其他人在浏览器中调用我的应用程序后,它工作得很好。
非常感谢@Ziggler的回答。我碰到的几乎所有研究都建议在IIS配置屏幕中修改web.config和launchSettings.json以及"启用Windows身份验证"和"禁用匿名身份验证",但这都不能解决问题。以下确实解决了我的问题...
将Windows身份验证的用户名拉出并清理干净,然后再返回值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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; } } |
必须为IIS站点单独启用Windows Auth。可能您尚未执行此操作,因此不会发生自动授权,因此用户名为null。
也就是说,您不应将
1 | HttpContext.User.Identity.Name |
使用:
1 | Helper.GetUserName(HttpContext) |
实际上,实际上您可以只使用