如何从自定义表中获取UserID

时间:2023-01-12 17:50:45

i would like to be able to get the ID of the Current user logged in on my Site.

我希望能够获取我的网站上登录的当前用户的ID。

The thing is that i'm not trying to get the Guid, but The User_ID, that is a field that i have on a custom table of mine called Users. On that table i have User_ID that is the PK and UserId that is a FK from the table aspnet_Users.

问题是我不是想要获得Guid,而是User_ID,这是我在我自己的一个名为Users的自定义表上的字段。在该表上,我有User_ID,它是PK和UserId,它是表aspnet_Users中的FK。

The reason i want this field is because i have a table Purchase that every time an User LOGGED IN presses the button saying (Buy), a new saleId is incremented and the User_ID that bought it. On my table Users the User_ID is of int type that starts at 1 and also increments every time a newUser registers on the site.

我想要这个字段的原因是因为我有一个表购买,每当用户登录按下按钮说(买)时,新的saleId会增加,而User_ID会购买它。在我的表上用户User_ID是int类型,从1开始,并且每次newUser在站点上注册时也会递增。

So it's easier to check an user by ID of (1,2,3) that an unique Identifier with 30 characters

因此,更容易通过ID(1,2,3)检查用户是否有30个字符的唯一标识符

What i have in mind is something like this:

我的想法是这样的:

protected void Button1_Click(object sender, EventArgs e)        
{
    string InsertSql = "INSERT INTO [Purchase] (User_ID) VALUES (@User_ID)";

            using (Connection)
            {

                Connection.Open();
                SqlCommand com = new SqlCommand(InsertSql, Connection);

                com.Paramaters.AddWithValue("@User_ID",  ????);

                com.ExecuteNonQuery();
                cn.Close(); 

             }
} 

Where is ???? i need to get somehow the User_ID of the current User logged in that is on my custom table Users.

哪里 ????我需要以某种方式得到我的自定义表用户登录的当前用户的User_ID。

Thanks for the help

谢谢您的帮助

5 个解决方案

#1


1  

After Log-in into your application you should maintain the userid into session variable ..so that later on you can use that directly.

登录到您的应用程序后,您应该将userid维护为会话变量..以后您可以直接使用它。

Example:-

例:-

    //while user logged-in,push userid into session variable:-

    Session["userid"] = 12;

    //And later on in oyur page you can use it like :-

   com.Paramaters.AddWithValue("@User_ID",  (int)Session["userid"]);

#2


0  

Assuming as you say that using is logged in

假设您说使用已登录

Use User.Identity.Name.

使用User.Identity.Name。

You check if user is authenticated using:

您使用以下方法检查用户是否经过身份验

User.Identity.IsAuthenticated

User.Identity.IsAuthenticated

If will give you the name of the current logged user in your asp.net application.

如果您将在asp.net应用程序中为您提供当前登录用户的名称。

com.Paramaters.AddWithValue("@User_ID",  User.Identity.Name);

#3


0  

If I understood right, you have a custom table Users, with PK User_ID and a column UserId that is a FK to the table aspnet_Users.

如果我理解正确,你有一个自定义表用户,具有PK User_ID和一列UserNd,它是表aspnet_Users的FK。

You already know the UserName of the logged in user, so why not simply do something like:

您已经知道登录用户的UserName,为什么不简单地执行以下操作:

string InsertSql = @"
    INSERT INTO [Purchase] (User_ID)
    SELECT U.User_ID FROM Users U
    INNER JOIN aspnet_Users AU ON U.UserId = AU.UserId
    WHERE AU.LoweredUserName = LCASE(@UserName)
     ";

Your @UserName parameter is then the name of the current logged on user that you already know (HttpContext.Current.User.Name), and you don't need to know the User_ID.

您的@UserName参数是您已知的当前登录用户的名称(HttpContext.Current.User.Name),您不需要知道User_ID。

#4


0  

If you are using Membership then

如果您正在使用会员资格

Gets the name if authenticated.

获取经过身份验证的名称。

if (User.Identity.IsAuthenticated)
    Label1.Text = HttpContext.Current.User.Identity.Name;
else
    Label1.Text = "No user identity available.";

If you want to get userid then run a select query like

如果你想获得用户ID,那么运行一个选择查询,如

Select userid from tablename where username = 'pass here username';

by running this query you will get userid and use that userid when you need.

通过运行此查询,您将获得userid并在需要时使用该用户ID。

Parametrized Query:

参数化查询:

com.Paramaters.AddWithValue("@User_ID",  HttpContext.Current.User.Identity.Name);

and if you are not using Membership then at the time of login get userid from your username and use any where you want to.

如果您没有使用会员资格,那么在登录时从您的用户名获取用户ID并使用您想要的任何地方。

Note: Make sure that you have unique username otherwise it will give you multiple userid.

注意:确保您拥有唯一的用户名,否则它将为您提供多个用户ID。

Logic 2: Use the ProviderUserKey member of the MembershipUser object, like this:

逻辑2:使用MembershipUser对象的ProviderUserKey成员,如下所示:

MembershipUser user = Membership.GetUser();
string userid = user.ProviderUserKey.ToString();

Hope it works..

希望它有效..

#5


0  

i finally did it, i kinda moved on to other stuff that i needed to do on my Site, so i just figured it out today.

我终于做到了,我有点转向我需要在我的网站上做的其他事情,所以我今天才想出来。

I just wanna say thanks for all the replies and leave the solution to my own problem. Basically what i was doing wrong was the Select command. I'm not very good at SQL, so i didn't know were to place it xD, but now i know.

我只想感谢所有回复,并将解决方案留给我自己的问题。基本上我错误的是Select命令。我不是很擅长SQL,所以我不知道是不是要把它放到xD,但现在我知道了。

protected void Button1_Click(object sender, EventArgs e)

protected void Button1_Click(object sender,EventArgs e)

{

{

    MembershipUser usr =  Membership.GetUser(HttpContext.Current.User.Identity.Name);
    Guid usrid = (Guid)usr.ProviderUserKey;

    String product= DropDownList1.SelectedValue.ToString();

    string InsertSql= "INSERT INTO [Purchase] (Product_ID, User_ID) VALUES (@Product_ID,(SELECT User_ID from [Users] where UserId = @UserId))";


        using (Connection)
        {
            Connection.Open();
            SqlCommand com = new SqlCommand(InsertSql, Connection);

            com.Parameters.AddWithValue("@Product_ID", product);
            com.Parameters.AddWithValue("@UserId", usrid);

            com.ExecuteNonQuery();
            Connection.Close();

        }

    Response.Redirect("Purchase.aspx");
} 

Once again, thanks for all the patience and help Take Care

再一次,感谢所有的耐心和帮助照顾

#1


1  

After Log-in into your application you should maintain the userid into session variable ..so that later on you can use that directly.

登录到您的应用程序后,您应该将userid维护为会话变量..以后您可以直接使用它。

Example:-

例:-

    //while user logged-in,push userid into session variable:-

    Session["userid"] = 12;

    //And later on in oyur page you can use it like :-

   com.Paramaters.AddWithValue("@User_ID",  (int)Session["userid"]);

#2


0  

Assuming as you say that using is logged in

假设您说使用已登录

Use User.Identity.Name.

使用User.Identity.Name。

You check if user is authenticated using:

您使用以下方法检查用户是否经过身份验

User.Identity.IsAuthenticated

User.Identity.IsAuthenticated

If will give you the name of the current logged user in your asp.net application.

如果您将在asp.net应用程序中为您提供当前登录用户的名称。

com.Paramaters.AddWithValue("@User_ID",  User.Identity.Name);

#3


0  

If I understood right, you have a custom table Users, with PK User_ID and a column UserId that is a FK to the table aspnet_Users.

如果我理解正确,你有一个自定义表用户,具有PK User_ID和一列UserNd,它是表aspnet_Users的FK。

You already know the UserName of the logged in user, so why not simply do something like:

您已经知道登录用户的UserName,为什么不简单地执行以下操作:

string InsertSql = @"
    INSERT INTO [Purchase] (User_ID)
    SELECT U.User_ID FROM Users U
    INNER JOIN aspnet_Users AU ON U.UserId = AU.UserId
    WHERE AU.LoweredUserName = LCASE(@UserName)
     ";

Your @UserName parameter is then the name of the current logged on user that you already know (HttpContext.Current.User.Name), and you don't need to know the User_ID.

您的@UserName参数是您已知的当前登录用户的名称(HttpContext.Current.User.Name),您不需要知道User_ID。

#4


0  

If you are using Membership then

如果您正在使用会员资格

Gets the name if authenticated.

获取经过身份验证的名称。

if (User.Identity.IsAuthenticated)
    Label1.Text = HttpContext.Current.User.Identity.Name;
else
    Label1.Text = "No user identity available.";

If you want to get userid then run a select query like

如果你想获得用户ID,那么运行一个选择查询,如

Select userid from tablename where username = 'pass here username';

by running this query you will get userid and use that userid when you need.

通过运行此查询,您将获得userid并在需要时使用该用户ID。

Parametrized Query:

参数化查询:

com.Paramaters.AddWithValue("@User_ID",  HttpContext.Current.User.Identity.Name);

and if you are not using Membership then at the time of login get userid from your username and use any where you want to.

如果您没有使用会员资格,那么在登录时从您的用户名获取用户ID并使用您想要的任何地方。

Note: Make sure that you have unique username otherwise it will give you multiple userid.

注意:确保您拥有唯一的用户名,否则它将为您提供多个用户ID。

Logic 2: Use the ProviderUserKey member of the MembershipUser object, like this:

逻辑2:使用MembershipUser对象的ProviderUserKey成员,如下所示:

MembershipUser user = Membership.GetUser();
string userid = user.ProviderUserKey.ToString();

Hope it works..

希望它有效..

#5


0  

i finally did it, i kinda moved on to other stuff that i needed to do on my Site, so i just figured it out today.

我终于做到了,我有点转向我需要在我的网站上做的其他事情,所以我今天才想出来。

I just wanna say thanks for all the replies and leave the solution to my own problem. Basically what i was doing wrong was the Select command. I'm not very good at SQL, so i didn't know were to place it xD, but now i know.

我只想感谢所有回复,并将解决方案留给我自己的问题。基本上我错误的是Select命令。我不是很擅长SQL,所以我不知道是不是要把它放到xD,但现在我知道了。

protected void Button1_Click(object sender, EventArgs e)

protected void Button1_Click(object sender,EventArgs e)

{

{

    MembershipUser usr =  Membership.GetUser(HttpContext.Current.User.Identity.Name);
    Guid usrid = (Guid)usr.ProviderUserKey;

    String product= DropDownList1.SelectedValue.ToString();

    string InsertSql= "INSERT INTO [Purchase] (Product_ID, User_ID) VALUES (@Product_ID,(SELECT User_ID from [Users] where UserId = @UserId))";


        using (Connection)
        {
            Connection.Open();
            SqlCommand com = new SqlCommand(InsertSql, Connection);

            com.Parameters.AddWithValue("@Product_ID", product);
            com.Parameters.AddWithValue("@UserId", usrid);

            com.ExecuteNonQuery();
            Connection.Close();

        }

    Response.Redirect("Purchase.aspx");
} 

Once again, thanks for all the patience and help Take Care

再一次,感谢所有的耐心和帮助照顾