• 21-08-2020, 17:21:55
    #1
    Merhabalar ASP.NET ile login form hazırladım login olmadan sayfaya erişmeye çalıştığında giriş formuna geri gönderiyorum ama bu hatayı alıyorum

    Session'a UserID oluşturduğum kodlar

     public bool IsLoginSuccess(string user, string pass)
    {
    User resultUser = db.Users.Where(x => x.userName.Equals(user) && x.password.Equals(pass)).FirstOrDefault();
    
    if (resultUser != null)
    {
    HttpContext.Current.Session.Add("UserID", resultUser.id.ToString());
    return true;
    }
    return false;
    }
    Patlayan kodum
     public class ControlLogin:ActionFilterAttribute,IActionFilter
    {
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
    if(!string.IsNullOrEmpty(HttpContext.Current.Session["UserID"].ToString()))
    {
    base.OnActionExecuting(filterContext);
    }
    else
    {
    HttpContext.Current.Response.Redirect("/login/LoginScreen");
    }
    base.OnActionExecuting(filterContext);
    }
        }
  • 21-08-2020, 21:34:16
    #2
    if(!string.IsNullOrEmpty(HttpContext.Current.Sessi on["UserID"].ToString()))
    Yerine
    if ((HttpContext.Current.Session["UserID"] != null) && (!string.IsNullOrEmpty(HttpContext.Current.Session["UserID"].ToString())))
    Yapman lazım. Yani önce null kontrolü yapmalısın
  • 21-08-2020, 21:42:27
    #3
    Şu satır;
    if(!string.IsNullOrEmpty(HttpContext.Current.Session["UserID"].ToString()))
    Şöyle olacak;
    if(!string.IsNullOrEmpty(HttpContext.Current.Session["UserID"]))
    Null bir değere ToString uygularsanız hata alırsınız. Zaten if de null ve empty olma durumunu sorguluyorsunuz.
  • 24-08-2020, 09:45:51
    #4
    omerk2 adlı üyeden alıntı: mesajı görüntüle
    if(!string.IsNullOrEmpty(HttpContext.Current.Sessi on["UserID"].ToString()))
    Yerine
    if ((HttpContext.Current.Session["UserID"] != null) && (!string.IsNullOrEmpty(HttpContext.Current.Session["UserID"].ToString())))
    Yapman lazım. Yani önce null kontrolü yapmalısın

    Teşekkür ederim cevabınız için ama anlamadığım şey isnullorempty komutu zaten null kontrolü yapmıyor mu? Neden null geldiğinde hataya düşüyor.
  • 24-08-2020, 09:46:35
    #5
    rshcoosl adlı üyeden alıntı: mesajı görüntüle
    Şu satır;
    if(!string.IsNullOrEmpty(HttpContext.Current.Session["UserID"].ToString()))
    Şöyle olacak;
    if(!string.IsNullOrEmpty(HttpContext.Current.Session["UserID"]))
    Null bir değere ToString uygularsanız hata alırsınız. Zaten if de null ve empty olma durumunu sorguluyorsunuz.
    Cevap içi teşekkür ederim ama isnullorempty string bir değer istediği için mecbur string vermek zorunda kaldım yukarıdaki arkadaşın cevabı çalıştı yine de çok teşekkür ederim.