Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

Context.User.Identity.IsAuthenticated is set to false even asffter cookie set

$
0
0

I am using the following code to authenticate my user which does work however when i go to check the isAunthenticated it is not being set to true.

      User _loginUser = _dal.VerifyPassword(txtUsername.Text, txtPassword.Text);
                    if (_loginUser == null)
                        lblerror.Text = "Invalid Login";
                    else
                    {
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                          1,                                     // ticket version
                          _loginUser.Email,                      // authenticated username
                          DateTime.Now,                          // issueDate
                          DateTime.Now.AddMinutes(30),           // expiryDate
                          chkRememberMe.Checked,
                          FormsAuthentication.FormsCookiePath);  // the path for the cookie

                        // Encrypt the ticket using the machine key
                        string encryptedTicket = FormsAuthentication.Encrypt(ticket);

                        // Add the cookie to the request to save it
                        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                        cookie.HttpOnly = true;
                        Response.Cookies.Add(cookie);
                        FormsAuthentication.SetAuthCookie(_loginUser.Email,true);
                
                        Response.Redirect(@"~\account\default.aspx");
                    }

Then on my page leavel at master page level i do the following check otherwise throw out the user

if (Context.User.Identity.AuthenticationType == "Forms" && Context.User.Identity.IsAuthenticated)
        {
            FormsIdentity _identity = (FormsIdentity)Context.User.Identity;
            _identity = (FormsIdentity)Context.User.Identity;

            userName = _identity.Name;

            User _myUser = _dal.getUser(userName);

            //we only want to add in the admin roles if the person themselfs is an admin
            if (_myUser.isAdmin == true)
            {//
             // chkAdmin.Checked = _myUser.isAdmin;
            }
        }
        else
        {
            Response.Redirect("~/Account/Login.aspx");
        }

However this line      if (Context.User.Identity.AuthenticationType == "Forms" && Context.User.Identity.IsAuthenticated) fails because of this value being set to false

Context.User.Identity.IsAuthenticated
  


Viewing all articles
Browse latest Browse all 4737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>