0

我是 asp.net 的新手,我有一个问题。在我的 asp.net web 目录中,我有一些用户页面和一个管理员文件夹,我想在我的 web.config 文件中使用一些标签,如身份验证和授权来确定我的管理员用户名和密码,并确定只有管理员可以使用页面在管理文件夹中,但我的问题是我没有在我的网络中使用任何登录控件让我的用户登录。我使用一些文本框来提供用户信息,然后我连接到我在数据库中的表并检查他们的信息,我想知道在这种情况下我该怎么做?如果有人帮助我,我会变得非常感激。用户使用他们的电子邮件和密码登录,这是我的代码:

       <tr>
        <td>
            &nbsp;Email :
        </td>
        <td class="style1">
            &nbsp;<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <br />

        </td>

    </tr>
     <tr>
        <td>
            &nbsp;
        </td>
        <td class="style1">
            &nbsp; <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                ErrorMessage="please enter your email!" 
                ControlToValidate="TextBox1" 
                Display="Dynamic" ForeColor="#CC0000" ValidationGroup="SignIn"></asp:RequiredFieldValidator>
            <br />
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="incorrect format!" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"  Display="Dynamic"  ForeColor="#CC0000" ControlToValidate="TextBox1" ValidationGroup="SignIn"></asp:RegularExpressionValidator>
           <br />

        </td>

    </tr>
    <tr>
        <td>
            &nbsp;password:
        </td>
        <td class="style1">
            &nbsp;<asp:TextBox ID="TextBox2" runat="server" TextMode="Password" ></asp:TextBox>
           <br />
        </td>

    </tr>
     <tr>
        <td>
            &nbsp;
        </td>
        <td class="style1">
         &nbsp;
            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Enter your password" ControlToValidate="TextBox2"  Display="Dynamic" ForeColor="#CC0000" ValidationGroup="SignIn" ></asp:RequiredFieldValidator>

        </td>

    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
        <td class="style1">
            &nbsp;<asp:Button ID="Button2" runat="server" Text="enter" 
                Width="123px" onclick="Button2_Click" ValidationGroup="SignIn" />
        </td>

我已经改变了它:

    protected void Button2_click(object sender, EventArgs e)
   {
     if (Membership.ValidateUser(TextBox1.Text.Trim(), TextBox2.Text.Trim()))
    {
        if (Roles.IsUserInRole(TextBox1.Text.Trim(), "Admin"))
        {
            Response.Redirect("Admin/Default.aspx");
            Session["user"] = "admin";
        }
       // ad.Text = "admin";
    }
    else
    {
      int c = -1;
    SqlConnection cn2 = new SqlConnection();
    cn2.ConnectionString = "server = . ; database = mobile_store ; Trusted_Connection=true";
    DataTable tb = new  DataTable();

    SqlCommand cmd2 = new SqlCommand();
    cmd2.Connection = cn2;
    cmd2.CommandType = CommandType.StoredProcedure;
    cmd2.CommandText = "Check_Email_Pass";
    cmd2.Parameters.AddWithValue("@mail", TextBox1.Text.Trim());
    cmd2.Parameters.AddWithValue("@pass", TextBox2.Text.Trim());
    cmd2.Parameters.Add("@res", SqlDbType.Int);    
    cmd2.Parameters["@res"].Direction = ParameterDirection.Output;

    SqlDataAdapter da = new SqlDataAdapter(cmd2);
    da.Fill(tb);
    try
    {
        cn2.Open();
        cmd2.ExecuteNonQuery();
        c = Convert.ToInt32(cmd2.Parameters["@res"].Value);

        switch (c)
        {
            case 1:
                {

                    Session["user"] = tb.Rows[0][0].ToString() + " " + tb.Rows[0][1].ToString();
                    Session["authenticate"] = true;
                    Session["id"] = Convert.ToInt32( tb.Rows[0][2]);

                    ((MasterPage)this.Master).lable2Visible = Session["user"].ToString();
                    Label2.Text = "Welcome" + " " + tb.Rows[0][0].ToString() + " " + tb.Rows[0][1].ToString();
                    TextBox1.Text = null;
                    if (Session["pagesource"] != null)
                    {
                        Response.Redirect((string)Session["pagesource"]);
                    }
                    else
                    {
                        Response.Redirect("~/user_page.aspx");
                    }

                }
                break;
            case 0:
                {
                    Label2.Text = "your password is wrong";

                }
                break;
            case 2:
                {

                    Label2.Text = "this email has registered before <br/> please sign up first";
                    TextBox3.Text = TextBox1.Text.Trim();
                }
                break;
            default:
                break;
        }

      }
    }

    catch (Exception ex)
    {
        Label1.Text = ex.ToString();

    }
    finally { cn2.Close(); }
   }

在我的 web.config 文件中

     <system.Web>
    <authentication mode="Forms">
        <forms  timeout="20" slidingExpiration="true" cookieless="AutoDetect" protection="All" requireSSL="false" enableCrossAppRedirects="false" loginUrl="entrance_before_paying.aspx" >
            <credentials passwordFormat="Clear">
                <user name="elmiragolshanff@yahoo.com" password="elmira" />
            </credentials>
        </forms>
    </authentication>
    </system.web>     


        <location path="Admin">
    <system.web>
        <authorization>
            <allow users="elmiragolshanff@yahoo.com" roles="Admin" />              
        </authorization>

</location>

    </system.web>
4

2 回答 2

1

埃尔迈拉,

有几种方法可以做到这一点;不过,@SLaks 对他的担忧是正确的。这是一篇可能在这方面对您有所帮助的帖子,“C# 中的哈希和盐密码”。

C# 中的哈希和盐密码

也就是说,我认为您会从阅读有关 ASP.NET 用户管理的内容中受益:“通过使用成员资格管理用户”。

http://msdn.microsoft.com/en-us/library/tw292whz(v=vs.100).aspx

它将允许您完全按照自己的意愿行事 - 根据配置文件限制对资源(页面)的访问。

于 2013-01-02T19:44:12.437 回答
0

我改变了它。现在它工作正常 web.configfile:

<authentication mode="Forms">
        <forms  timeout="30" loginUrl="~/entrance_before_paying.aspx" defaultUrl="Admin/Default.aspx" name=".ASPXFORMSDEMO"  cookieless="AutoDetect" protection="All"   >

        <credentials passwordFormat="Clear">
                <user name="elmiragolshanff@yahoo.com"  password="elmira" />
            </credentials>
        </forms>
    </authentication>

我在 entry_before_paying.aspx 页面中的 button_click 事件:

if (FormsAuthentication.Authenticate(TextBox1.Text.Trim(), TextBox2.Text.Trim()))
   {

           Response.Redirect("Admin/Default.aspx");
            Session["user"] = "admin";     

   }
 else{ //I wrote the code to authenticate users ....}
于 2013-01-07T16:45:50.330 回答