【问题标题】:Get User.Identity.GetUserId in SQL querySQL查询中获取User.Identity.GetUserId
【发布时间】:2016-02-21 18:50:13
【问题描述】:

试图获取当前登录的用户用户 ID,并在后台代码的 SQL 查询中使用它来填充网格视图,但它没有任何想法我做错了什么。我不断收到空值错误。另外,如果可能的话,我会尝试仅使用 Microsoft.AspNet.Identity

页面加载

if (!IsPostBack)
    {
        SqlDataSource SqlDataSource1 = new SqlDataSource();
        SqlDataSource1.ID = "SqlDataSource1";
        this.Page.Controls.Add(SqlDataSource1);
        SqlDataSource1.InsertParameters["luser"].DefaultValue = User.Identity.GetUserId();
        SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        SqlDataSource1.SelectCommand = "SELECT top 3 UserId, Friending from Friendreq Where Friending = @luser";
        GridView1.DataSource = SqlDataSource1;
        GridView1.DataBind();
    }

更新:

通过将请求直接添加到 SQL 查询中,我能够得到我想要的结果。有什么理由我不应该添加它吗

 SqlDataSource SqlDataSource1 = new SqlDataSource();
    SqlDataSource1.ID = "SqlDataSource1";
    this.Page.Controls.Add(SqlDataSource1);

    SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    SqlDataSource1.SelectCommand = "SELECT top 3 UserId, Friending from Friendreq Where Friending = '"+User.Identity.GetUserId()+"'";
    GridView1.DataSource = SqlDataSource1;
    GridView1.DataBind();

解决方案 = Friending = '"+User.Identity.GetUserId()+"'";

【问题讨论】:

  • @Andrei 我在 SqlDataSource1.InsertParameters["luser"].DefaultValue = User.Identity.GetUserId(); 行上得到错误,错误是“异常App_Web_3lu2kaoo.dll 中发生了“System.NullReferenceException”类型,但未在用户代码中处理”

标签: c# sql asp.net webforms


【解决方案1】:

如果尝试从 HttpContext 获取用户身份,请尝试以下代码。 Name 属性中的身份存储用户名/ID(基于您的逻辑)。

对象引用错误也即将到来,因为您没有在 InsertParameter 中添加“luser”,而是试图为其设置默认值。我已经添加了这个SqlDataSource1.InsertParameters.Add(new Parameter("luser"));,以确保它有参数,然后再使用它

if (!IsPostBack)
    {
        SqlDataSource SqlDataSource1 = new SqlDataSource();
        SqlDataSource1.ID = "SqlDataSource1";
        this.Page.Controls.Add(SqlDataSource1);
        SqlDataSource1.InsertParameters.Add(new Parameter("luser"));
        SqlDataSource1.InsertParameters["luser"].DefaultValue = HttpContext.Current.User.Identity.IsAuthenticated ? HttpContext.Current.User.Identity.Name : String.Empty;
        SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        SqlDataSource1.SelectCommand = "SELECT top 3 UserId, Friending from Friendreq Where Friending = @luser";
        GridView1.DataSource = SqlDataSource1;
        GridView1.DataBind();
    }

【讨论】:

  • 我仍然遇到同样的错误。我也使用此代码作为页面加载。所以我想这可能与这个问题有关。我是编码新手,所以我不确定。
  • 您使用的是什么类型的身份验证?
  • 表单身份验证 (Microsoft.AspNet.Identity)
  • 我遇到了问题。它与 User.Identity 无关,但您得到空引用,因为 SqlDataSource1.InsertParameters["luser"] 为空,并且之后的 .DefaultValue 将引发对象引用错误
  • 我已经更新了代码,在你使用之前添加了参数
猜你喜欢
  • 1970-01-01
  • 2014-10-26
  • 2013-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-14
  • 2011-02-23
相关资源
最近更新 更多