简体   繁体   English

从用户名和密码输入中获取主键

[英]Getting the primary key from a username and password entry

I have a table which consists of id username and password. 我有一个包含ID用户名和密码的表。 I currently have a wpf form which asks the user for username and password and if they exist in the table the person can log in. After they have logged in it leads them to another table and form in which they can add data. 我目前有一个wpf表单,它要求用户输入用户名和密码,并且该用户是否可以在表中登录,因此该人可以登录。登录后,它将引导他们进入另一个表和表单,可以在其中添加数据。

My question is how can i get the pk of the login information used and add it it to the table on the next form? 我的问题是如何获取所用登录信息的pk并将其添加到下一张表格的表格中?

Edit: on the second table i already have a column dedicated for the pk of the username and password table Edit2: 编辑:在第二个表上,我已经有一个专用于用户名和密码表Edit2的pk的列:

The table which stores the login information is as follows 存储登录信息的表如下

ID - username - password
1   - ish - 1234
2  - tom - abc
3  - jeff - qwerty
etc

i want to be able to retrieve the id when i login in though two textbooks 我希望能够通过两本教科书登录时检索ID

eg if i type "tom" then type "abc" i should be able to the id of 2 例如,如果我键入“ tom”,然后键入“ abc”,则我的ID应该为2

Well you get the id from which they log in and pass it to the other form: 好了,您可以获得他们登录时使用的ID,并将其传递给另一种形式:

using(Sqlconnection con = new SqlConnection(constr))
{
     using(SqlCommand cmd = new SqlCommand("select id from Users where username = @username and password = @password", con)
     {
          cmd.Paramters.Add("@username", SqlDbType.VarChar).Value = username;
          cmd.Paramters.Add("@password", SqlDbType.VarChar).Value = password;
          con.Open();
          string result = (string)cmd.ExecuteScalar();
          if(result!=null)
          {
              int id = int.Parse(result);
              SecondForm frm = new SecondForm();
              frm.UserId = id;
              frm.Show();
          }
          else
          {
              //tell the user that credentials are not valid
          }
     }
}

In your second form you should have a property like: 在第二种形式中,您应具有以下属性:

public int UserId {get; set;} = 0;

In this case it is better to write a stored procedure that takes username & password and returns the primary key if valid else -1. 在这种情况下,最好编写一个存储过程,该过程使用用户名和密码并返回主键(如果有效,则返回-1)。 Call the the SP from the code and if the return value is greater than 0 then it is the primary key. 从代码中调用SP,如果返回值大于0,则它是主键。

How you pass this id to next form depends on the technology you use. 您如何将此ID传递到下一个表格取决于您使用的技术。 It can be passed as query string, not recommended though, post as form field, Session variable, etc. 可以将其作为查询字符串传递,但不建议将其传递为表单字段,会话变量等。

There are lot of scope for improvement in this design though. 但是,此设计还有很多改进的余地。 But it is better to follow some basic things like 但是最好遵循一些基本的东西,例如

In the second table the column that stores the primary key (id) of first table should be a foreign key referencing primary key of first table. 在第二个表中,存储第一个表的主键(id)的列应该是引用第一个表的主键的外键。 Technically it doesn't have to be a foreign key but it is best approach. 从技术上讲,它不一定是外键,但这是最佳方法。

Another suggestion is to store the password as hash instead of direct text. 另一个建议是将密码存储为散列而不是直接文本。 A hash with salt is lot safer. 用盐哈希更安全。

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

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