こんにちは私は3段階でC#でログインフォームを最初から作成しています。私は、ユーザーデータが正しいかどうかをチェックする作業フォームを構築することに成功しました。彼が間違ったデータを記入すると、彼はメッセージを受け取るでしょう。しかし、今私はIDを格納するセッションを作成する必要があります。C#でセッションを作成
ウェブを検索しましたが、Session["sessionName"]= data
を追加する必要がありますが、Session["userId"]=s.studentNummer
と入力すると何も認識されません。セッションをDALまたはDLLに入れる方が良いでしょうか?私はDAL(function checkLogin)に書きたいと思っていました。誰か助けてくれますか?ここで
は私のコードです:セッションへ
DALstudent.cs
public class DALstudent
{
dc_databankDataContext dc = new dc_databankDataContext();
public void insertStudent(Student s)
{
dc.Students.InsertOnSubmit(s);
dc.SubmitChanges();
}
public bool checkLogin(string ID, string passw)
{
bool canlogin = false;
var result = (from s in dc.Students
where s.studentNummer == ID && s.studentPasswoord == passw
select s).Count();
if (result == 1)
{
canlogin = true;
}
else
{
canlogin = false;
}
return canlogin;
}
}
BLLstudent.cs
public class BLLstudent
{
DALstudent DALstudent = new DALstudent();
public void insertStudent(Student s)
{
DALstudent.insertStudent(s);
}
public string getMD5Hash(string passwd)
{
MD5CryptoServiceProvider x = new MD5CryptoServiceProvider();
byte[] bs = Encoding.UTF8.GetBytes(passwd);
bs = x.ComputeHash(bs);
StringBuilder str = new StringBuilder();
foreach (byte b in bs)
{
str.Append(b.ToString("x2").ToLower());
}
string password = str.ToString();
return password;
}
public bool checkLogin(string ID, string passw)
{
bool canlogin = DALstudent.checkLogin(ID, passw);
if (canlogin == true)
{
return true;
}
else
{
throw new Exception("Uw gegevens kloppen niet");
}
}
}
login.aspx.cs
public partial class web_login : System.Web.UI.Page
{
protected void btnLogin_Click(object sender, EventArgs e)
{
try
{
BLLstudent BLLstudent = new BLLstudent();
var loginNr = txtLoginNr.Text;
var pass = BLLstudent.getMD5Hash(txtWachtwoord.Text);
var passw = pass;
BLLstudent.checkLogin(loginNr, passw);
Response.Redirect("student/s_procedure_goedkeuring.aspx");
}
catch (Exception Ex)
{
lblFeedback.Text = Ex.Message;
}
}
}
を「彼は何も認識しません。」で詳しく説明してくださいまた、セッション状態はDALまたはBLLには入りません。それは、Webアプリケーションに正当に属します。 – David
あなたのユーザーIDは、ユーザーがログインに使用するIDと同じですか?はいの場合、セッションに格納する必要なしに、IDデータが必要なときはいつでも、HttpContext.Current.User.Identity.Name.ToString()を使用して直接アクセスできます。 –
MD5は安全なハッシュではありません。 SHA1またはSHA2を使用し、塩分を忘れないでください。 – Bengie