0
私のデータベースに接続しているログインページを作成しようとしていました。ユーザーが無効であることを示しています。何が問題なのか分かりますか?ありがとう!ここ は完全なコードです:最初に、あなたの質問にユーザー名が無効です。データベース(ASP.net)からユーザー名を読み取ることができません
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ToString());
conn.Open();
string checkuser = " select count(*) from [Table] where UserName = ' " + TextBoxUserName.Text + "' and Password='"+ TextBoxPassword.Text + "' ";
SqlCommand com = new SqlCommand(checkuser, conn);
int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
if (temp == 1)
{
Session["user"] = TextBoxUserName.Text;
Response.Write("Login success");
}
else
{
Response.Write("Login fail");
}
}
'user'というセッションがありますか?完全なエラーメッセージをご提供いただけますか? – Dumisani
ユーザは '[Table]'に格納されているのですか?これはサンプルコードの単なるプレースホルダですか?サイドノードとして、ユーザー入力を直接SQLクエリに連結することはなく、[SQLインジェクション](https://www.owasp.org/index.php/SQL_Injection)を防ぐために 'SqlParameter'を使用します(たとえば、TextBoxPasswordを' ; DROP TABLE [テーブル]; ') –
PS:DBにプレーンテキストとしてパスワードを保存しているようですね?自社開発のソリューションではなく、組み込みの[ASPメンバシップ](https://msdn.microsoft.com/en-us/library/yh26yfzy.aspx)を使用してログインする方が安全です。 –