0

テーブルts_deptからC#で記述された次のSQLクエリを使用してdeptの値を取得しています-いつどのように割り当てるのSession["UserAuthentication"]です(CurrentName != null)か?

   protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        string username = Login_Box.UserName;
        string pwd = Login_Box.Password;

        string strConn;
        strConn = WebConfigurationManager.ConnectionStrings["team13ConnectionString"].ConnectionString;
        SqlConnection Conn = new SqlConnection(strConn);
        Conn.Open();

        string sqlUserName;
        sqlUserName = "SELECT dept FROM ts_dept WHERE id=@username AND pass=@pwd";

        SqlCommand com = new SqlCommand(sqlUserName, Conn);
        com.Parameters.Add("@username", username);
        com.Parameters.Add("@pwd", pwd);

        string CurrentName;
        CurrentName = (string)com.ExecuteScalar();

        if (CurrentName != null)
        {
            Session["UserAuthentication"] = username;
            Session.Timeout = 1;
            Response.Redirect("Default.aspx");
        }
        else
        {
            Session["UserAuthentication"] = "";
        }
    }
4

1 に答える 1

1

ここで(私のC#マシンではなく)メモリから移動しますが、これを試してください:

object CurrentName = com.ExecuteScalar();
if (CurrentName != null && CurrentName != System.DBNull.Value) {
    {
        Session["UserAuthentication"] = (string)CurrentName;
        Session.Timeout = 1;
        Response.Redirect("Default.aspx");
    }
    else
    {
        Session["UserAuthentication"] = "";
    }
}

私の記憶が正しければ、クエリが結果を返さない場合は CurrentName が null になり、クエリが結果を返すが ts_dept.dept が null の場合は System.DBNull.Value になります。

また、認証にセッションを使用することに関するコメントにも注意してください。負荷分散されたクラスターにいる場合は、まったく機能しません。これをフォーム認証に切り替え始めます。

于 2013-03-23T18:07:58.503 に答える