사용자 이름과 비밀번호가있는 경우 DB에 간단한 인증 유형이 있습니다. creat FormsAuthenticationTicket 및 HttpContext.Current.Response.Cookies.Add (cookie); 요청 전에 HttpContext.Current.User.Identity.IsAuthenticated를 검사하면 false가 반환됩니다. 하지만 다음 요청에 대한 global.asax Application_AuthenticateRequest (개체 발신자, EventArgs e)를 확인한 경우 HttpContext.Current.User.Identity.IsAuthenticated true가 반환됩니다.문제를 해결하는 데 도움주세요 whith 맞춤형 인증
public static bool Login(string userName, string pass)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
try
{
conn.Open();
string comand = string.Format("Select * From ChatUser Where " +
"userName = '{0}' and pass ='{1}'", userName, pass);
SqlCommand cmd = new SqlCommand(comand, conn);
var reader = cmd.ExecuteReader();
if (!reader.HasRows)
{
conn.Close();
return false;
}
while (reader.Read())
{
string id = reader["id"].ToString();
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, id, DateTime.Now, DateTime.Now.AddMinutes(30),
false, null, FormsAuthentication.FormsCookiePath);
string hashCookies = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashCookies);
HttpContext.Current.Response.Cookies.Add(cookie);
}
conn.Close();
JoinMesage();
return true;
}
catch (Exception ex)
{
//write in log file
return false;
}
}
}
public static void JoinMesage()
{
string userId;
if (HttpContext.Current.User != null)
{
userId = HttpContext.Current.User.Identity.Name;
using (SqlConnection conn = new SqlConnection(connectionString))
{
string comand = string.Format("Insert into Messages (userID,mesageDate,userStatus)"
+ " Value('{0}','{1}','{2}')", userId, DateTime.Now, true);
}
}
}