【问题标题】:Please help solve issue whith custom autentification请帮助解决自定义身份验证问题
【发布时间】:2012-09-08 11:56:09
【问题描述】:

如果用户名和密码存在,我会在 DB 中检查简单的身份验证类型 我创建 FormsAuthenticationTicket 并将其添加 HttpContext.Current.Response.Cookies.Add(cookie);当我在请求之前检查 HttpContext.Current.User.Identity.IsAuthenticated 它返回 false 但是如果我在 global.asax Application_AuthenticateRequest(object sender, EventArgs e) 中检查以下请求 HttpContext.Current.User.Identity.IsAuthenticated 返回 true

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);
            }
        }


    }

【问题讨论】:

    标签: asp.net basic-authentication custom-authentication


    【解决方案1】:

    请修改您的票。应该是

    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
        1, 
        id, 
        DateTime.Now, 
        DateTime.Now.AddMinutes(30),
        true, 
        null, 
        FormsAuthentication.FormsCookiePath);
    

    并在自定义身份类中设置身份验证类型 请点击链接:

    http://www.asp.net/web-forms/tutorials/security/introduction/forms-authentication-configuration-and-advanced-topics-cs

    会解决的。

    【讨论】:

      猜你喜欢
      • 2014-10-19
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多