1

ログインページを認証できる簡単な方法。3層アーキテクチャでその認証を行うにはどうすればよいですか? DAL、BAL、および GUI レイヤーにあるべきコードを教えてください。これが私の簡単なコードです:

Web.config:

<authentication mode="form">
    <form loginurl="Login.aspx">
         <credential password Format="clear">
          <user name="abcd" password="1234">
        </credential>
      </authentication>
     </form>
   <authorization>
     <deny users="?">
   </authorization>

login.aspx.cs:

   sqlconnection con=new sqlconnection("server=localhost;database=dbname;uid=;pwd=;Trusted_Connection=true");
sqldataAdapter da=new sqldataAdapter("select * from Login where UserName='"+TextBox1.Text+"' and Password='"+TextBox2.Text+"'",con);
Dataset ds=new Dataset();
da.Fill(ds);

if(ds.Tables[0].rows.Count>0)
{
   if(FormAuthentication.Authenticate("abcd","1234")
   {
        FormAuthentication.RedirectFromLoginPage(TextBox1.Text,false);
        Response.write("Logged in");
    }
    else
    {
        Response.write("Unautherised User");
    }

   Response.Redirect("welcome.aspx");
}
else
{
  Response.write("Sorry Invalid UserName or Password");
}
4

2 に答える 2

1

一般に、少なくとも次のクラスが必要です。

  • DALでは、データベース接続を処理するクラスが必要です
  • BAl では、すべてのユーザー インスタンスを表すクラスが必要です。このクラスには、すべての認証と承認が行われる login() というメソッドが必要です。
  • ユーザー インターフェイスを表す Web フォーム。

また、SQL インジェクションを防ぐために、クエリ文字列を連結しないでください。代わりにパラメーターを使用してください。

クラスの例を次に示します。

namespace DAL
{
    public class ConnectionManager
    {
        public static SqlConnection GetConnection() {
            SqlConnection cn = new SqlConnection("server=localhost;database=dbname;uid=;pwd=;Trusted_Connection=true");
            cn.Open();
            return cn;
        }
    }
}

namespace BAL
{
    public class User
    {
        public string UserName { get; set; }
        public string Password { private get; set; }

        public bool Login() {
            return Login(this.UserName, this.Password);
        }

        public bool Login(string user, string password) {
            bool success=false;
            using (SqlConnection cn = ConnectionManager.GetConnection())
            {
                string sql = "select count(*) from Login where UserName=@user and Password=@password";
                using (SqlCommand command = new SqlCommand(sql, cn))
                {
                    command.Parameters["@user"].Value = user;
                    command.Parameters["@password"].Value = password;
                    success = (int)command.ExecuteScalar() > 0;
                }
                cn.Close();
            }
            return success;
        }
    }
}
于 2009-01-28T06:50:55.110 に答える
0

なぜ車輪を再発明したいのか、少し途方に暮れていますか? ASP.NET メンバーシップ プロバイダーはこれをすべて行います。その動作を大幅に変更する必要がある場合は、読みやすく、理解しやすく、変更しやすいオープン ソースです。独自の n 層アーキテクチャと簡単に統合できます。これは常に行っています。

于 2009-01-28T07:33:08.940 に答える