简体   繁体   English

在SQL查询中获取User.Identity.GetUserId

[英]Get User.Identity.GetUserId in SQL query

Trying to get currently logged in users userid and use it in an SQL query from behindcode to fill a gridview but its not working any ideas what I'm doing wrong. 试图获取当前登录的用户userid并在来自backcode的SQL查询中使用它来填充gridview,但是它不能解决我正在做错的任何事情。 I keep getting a null value error. 我不断收到空值错误。 Also I'm trying to only use the Microsoft.AspNet.Identity if thats even possible 我也试图只使用Microsoft.AspNet.Identity

pageload 页面加载

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

UPDATE: 更新:

I was able to get the results that i wanted by adding the requested directly into the SQL query. 通过将请求直接添加到SQL查询中,我可以获得所需的结果。 Is there any reason that i should not add it that what 有什么理由我不应该添加它吗?

 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();

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

If are trying to get user identity from HttpContext then try following code instead. 如果要尝试从HttpContext获取用户身份,请尝试使用以下代码。 Identity store username/id (based on your logic) in Name property. 名称属性中的身份存储库用户名/标识(基于您的逻辑)。

Also the object reference error is coming because you have not added "luser" in InsertParameter, but trying to set default value for it. 另外,对象引用错误即将到来,因为您没有在InsertParameter中添加“ luser”,而是尝试为其设置默认值。 I have added this SqlDataSource1.InsertParameters.Add(new Parameter("luser")); 我添加了此SqlDataSource1.InsertParameters.Add(new Parameter("luser")); to make sure it have the parameter, before you use it 在使用前确保它具有参数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM