2011-12-21 20 views
0

在我的项目中,我实现了登录。
页login.aspx的页面包含:如何使标准登录与web.config不相关?

<asp:Login ID="MainLogin" runat="server" onloggingin="MainLogin_LoggingIn"></asp:Login> 

在服务器端此页:我需要后来我写到会话变量

protected void MainLogin_LoggingIn(object sender, LoginCancelEventArgs e) 
{ 
    if (FormsAuthentication.Authenticate(MainLogin.UserName, MainLogin.Password)) 
    { 
     FormsAuthentication.RedirectFromLoginPage(MainLogin.UserName, MainLogin.RememberMeSet); 
     if (FormsAuthentication.Authenticate(MainLogin.UserName, MainLogin.Password)) 
     { 
      SqlConnection myConnection = new SqlConnection(DBConnection.GetConnectionString()); 
      SqlCommand myCommand = new SqlCommand("SELECT * FROM Users WHERE [email protected] and [email protected]", myConnection); 
      myCommand.Parameters.AddWithValue("login", MainLogin.UserName); 
      myCommand.Parameters.AddWithValue("pass", MainLogin.Password); 
      myCommand.Connection.Open(); 
      SqlDataReader Reader = myCommand.ExecuteReader(); 
      while (Reader.Read()) 
      { 
       Session["curUserRole"] = Reader["role"].ToString(); 
       Session["curUserLogin"] = MainLogin.UserName; 
       Server.Transfer(Request.Url.LocalPath); 
       FormsAuthentication.RedirectFromLoginPage(MainLogin.UserName, MainLogin.RememberMeSet); 
       return; 
      } 
      //Reader.Close(); 
      //myCommand.Connection.Close(); 
      //myConnection.Close();      
     } 
     else { 

     }   
    } 

变量。 在web.config中我有:

<authentication mode="Forms"> 
     <forms loginUrl="Login.aspx"> 
     <credentials passwordFormat="Clear"> 
      <user name="Admin" password="sa" /> 

     </credentials>   
     </forms> 
    </authentication> 
    <authorization> 
     <allow roles="Admin"/> 
     <deny users="?"/> 
    </authorization> 

所以我输入登录 - “管理”,并通过 - “SA”,然后将其签入web.config中,如果发现项目的用户它在数据库检查该项目的,我只需要在数据库中检查项目,如果有一个加载启动页面,则再次加载Login.aspx。

+0

真的不知道你想要做什么,但是这不是真的,你看加密的配置部分检查用户名登录的一种好方法,或存储用户名密码在数据库中加密,而不是在清除文本中。?从你有什么你正在尝试做基本表单身份验证是否正确另外你怎么没有检查,看看你有没有从数据库查询返回任何值之前分配值的会话对象这就是我们所有的负面假设 – MethodMan 2011-12-21 20:48:59

+0

做不以纯文本格式存储密码。你应该使用内置的SqlMembershipProvider。 – SLaks 2011-12-21 20:51:03

+0

现在它明文,加密我关闭。稍后我可以使用该用户类型管理加密通行证或从数据库登录。 Bu我不需要在web.confog中实际编写任何代码 – 2011-12-21 20:53:11

回答

相关问题