Web开发问题:HttpContext.Current.User.Identity.Name为null

本文介绍了一种在Web开发中遇到的问题,即在调试登录界面时无法通过HttpContext.Current.User.Identity.Name获取本机登录名。文章详细解释了如何通过启用Windows身份验证来解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

做web登陆界面,调试时需要获取本机登陆名

  public static string GetCurrentNTID()
  {
      string identityName = HttpContext.Current.User.Identity.Name;
      int splitIndex = identityName.IndexOf('\\');
      return splitIndex > -1 ? identityName.Substring(splitIndex + 1) : identityName;
  }

但是HttpContext.Current.User.Identity.Name;获取到的对象一直为null

解决方法:

设置文件属性如下:
将windows authentication属性设为enable,得以解决。
在这里插入图片描述

using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Web.Services; using System.Web.Script.Services; namespace WebApplication1 { public partial class WebForm2 : System.Web.UI.Page { private readonly List<Product> _products = new List<Product> { new Product { Id = 1, Name = "专业减震跑步鞋", Category = "运动鞋", Price = 599, ImageUrl = "image/专业减震跑步鞋.jpg", Brand = "耐克", Description = "专业级减震科技,适合长距离跑步" }, new Product { Id = 2, Name = "专业篮球鞋", Category = "运动鞋", Price = 699, ImageUrl = "image/专业篮球鞋.jpg", Brand = "阿迪达斯", Description = "高帮支撑设计,保护脚踝" }, new Product { Id = 3, Name = "多功能运动背包", Category = "运动装备", Price = 299, ImageUrl = "image/多功能运动背包.jpg", Brand = "彪马" }, new Product { Id = 4, Name = "专业运动智能手表", Category = "运动配件", Price = 799, ImageUrl = "image/专业运动智能手表.jpg", Brand = "Apple" }, new Product { Id = 5, Name = "速干透气运动T恤", Category = "运动服饰", Price = 199, ImageUrl = "image/速干透气运动T恤.jpg", Brand = "耐克" }, new Product { Id = 6, Name = "弹性运动短裤", Category = "运动服饰", Price = 159, ImageUrl = "image/弹性运动短裤.jpg", Brand = "阿迪达斯" } }; // 用户实体类 public class User { } // 商品实体类 public class Product { public int Id { get; set; } public string Name { get; set; } public string Category { get; set; } public decimal Price { get; set; } public string ImageUrl { get; set; } public string Description { get; set; } public string Brand { get; set; } } public class CartItem { public int ProductId { get; set; } public string ProductName { get; set; } public decimal Price { get; set; } public int Quantity { get; set; } public string ImageUrl { get; set; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // 记录用户浏览记录(示例) if (Session["UserID"] != null) { int userId = Convert.ToInt32(Session["UserID"]); LogUserBrowse(userId, 1); // 假设浏览了商品ID=1 } } } private List<Product> GetRecommendedProducts() { List<Product> products = new List<Product>(); if (Session["UserID"] == null) return products; int userId = Convert.ToInt32(Session["UserID"]); string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; // 读取配置 using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); string sql = @"SELECT p.*, c.CategoryName, b.BrandName FROM tb_Product p JOIN tb_Category c ON p.CategoryID = c.CategoryID JOIN tb_Brand b ON p.BrandID = b.BrandID WHERE p.CategoryID IN ( SELECT DISTINCT CategoryID FROM tb_Product WHERE ProductID IN ( SELECT ProductID FROM tb_UserBrowse WHERE UserID=@UserID ) ) OR p.BrandID IN ( SELECT DISTINCT BrandID FROM tb_Product WHERE ProductID IN ( SELECT ProductID FROM tb_UserBrowse WHERE UserID=@UserID ) )"; using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddWithValue("@UserID", userId); using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { // 填充 Product 对象... } } } } return products; } private void LogUserBrowse(int userId, int productId) { } } } 根据已有的前后端代码更新改善后端代码,给出完整的后端代码
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值