Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
[Authorize]
        public ActionResult Home()
        {

            return View();
        }
        public ActionResult Login()
        {
            return View();
        }
       
        [HttpPost]
        
        public ActionResult Login(User_info ui,string returnUrl)
        {
            if (ModelState.IsValid)
            {
                using (CRM_DBEntities crm = new CRM_DBEntities())
                {
                    var details = crm.User_info.Where(a => a.Username.Equals(ui.Username) && a.Password.Equals(ui.Password)).FirstOrDefault();

                    if (details != null)
                    {
                        FormsAuthentication.SetAuthCookie(details.Username, false);
                        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                    && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return RedirectToAction("Home", "crm");
                        }
                        //Session["Username"] = details.Email.ToString();
                        //return RedirectToAction("Home","crm");

                    }
                    else
                    {
                        ModelState.AddModelError("", "The user name or password         provided is incorrect.");
                        return View();
                    }

                }
            }


            return View();
        }
        [Authorize]
        public ActionResult Logout()
        {

            FormsAuthentication.SignOut();
            return RedirectToAction("Index","Home");
        }


What I have tried:

<system.web>
    <authentication mode="Forms">
      <forms loginUrl="~/crm/Login" defaultUrl="~/" timeout="20" slidingExpiration="true"/>
    </authentication>
Posted
Updated 17-Feb-17 2:13am
v3
Comments
F-ES Sitecore 17-Feb-17 6:16am    
You need to implement the "remember me" feature when you login, I don't have the code to hand but I believe it's a property you add to the method to log the person in as to if you want the login to be persisted or not. That should keep them logged in until they log out.
jay gandhi 17-Feb-17 6:48am    
i have also tried FormsAuthentication.SetAuthCookie(details.Username, true); but still same issue arrised ... what can i do other
Richard Deeming 17-Feb-17 11:37am    
You're storing passwords in plain text. Don't. You should only ever store a salted hash of the password, using a unique salt per record.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

If you used the built-in Identity[^] framework, rather than trying to roll your own, it would take care of this sort of thing for you.

1 solution

Change

FormsAuthentication.SetAuthCookie(details.Username, false);


to

FormsAuthentication.SetAuthCookie(details.Username, true);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900