Ciao Sto creando un modulo di accesso da zero in C# utilizzando 3 livelli. Sono riuscito a creare un modulo di lavoro che controlla se i dati dell'utente sono corretti. Se ha inserito dati errati, riceverà un messaggio. Ma ora ho bisogno di creare una sessione per memorizzare l'id.Crea sessione in C#
Ho cercato sul Web e dicono che è necessario aggiungere Session["sessionName"]= data
, ma se digito Session["userId"]=s.studentNummer
non riconosce nulla. È meglio mettere le sessioni nel DAL o nella DLL? Volevo scriverlo nel DAL (function checkLogin). Qualcuno può aiutarmi?
Ecco il mio codice:
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;
}
}
}
Si prega di elaborare su Inoltre, lo stato della sessione non va nel DAL o nel BLL. Appartiene direttamente nell'applicazione web. – David
è il tuo id utente identico a quello che l'utente usa per accedere? in caso affermativo, è possibile accedervi direttamente tramite HttpContext.Current.User.Identity.Name.ToString() ogni volta che si desiderano dati ID, senza necessità di memorizzarli nella sessione –
MD5 non è un hash sicuro. Utilizzare SHA1 o SHA2 e non dimenticare di salare. – Bengie