c# - FormAuthentication .ASPXAUTH cookie showing null after few moments in ASP.NET MVC -
i have implemented formauthentication in asp.net mvc 5 , create formsauthenticationticket on login , creates after few moments cookie showing in browser in application it's getting null.
please solve issue.
any appreciated thanks
login form
public actionresult login([bind(include = "username, password")] loginmodel loginmodel, string returnurl) { if (modelstate.isvalid) { egov_users egov_users = db.egov_users .where(p => p.usertype.type != "o" && p.username == loginmodel.username) .firstordefault(); if (egov_users == null) { modelstate.addmodelerror("", "invalid username"); return view(); } else { if (egov_users.password != loginmodel.password) { modelstate.addmodelerror("", "invalid password"); return view(); } var logindetail = new logindetails(); var serializer = new javascriptserializer(); logindetail.userid = egov_users.userid; logindetail.username = egov_users.username; logindetail.firstname = egov_users.firstname; logindetail.lastname = egov_users.lastname; var userdata = serializeuserinfointernal(logindetail); formsauthentication.setauthcookie(logindetail.username, false); var cookie = formsauthentication.getauthcookie( formsauthentication.formscookiename, false); var ticket = formsauthentication.decrypt(cookie.value); var durationinhours = 8; formsauthenticationticket newticket = new formsauthenticationticket( ticket.version, logindetail.username, datetime.now, datetime.now.addhours(durationinhours), true, userdata); // encrypt ticket. string encticket = formsauthentication.encrypt(newticket); cookie.value = encticket; response.cookies.add(cookie); int cookiesize = system.text.utf8encoding.utf8.getbytecount(cookie.values.tostring()); session["cookiesize"] = cookiesize; if (string.isnullorempty(returnurl)) { return redirecttoaction("index", "users"); } } } return redirecttoaction("login", "login"); }
global asax
protected void application_postauthenticaterequest() { var ticket = getticketfromcurrentcookie(); if (ticket == null) { global.writelog("application_postauthenticaterequest", "ticket becomes null"); return; } var user = deserializeuserinfointernal(ticket.name, ticket.userdata); if (user == null) { return; } var principal = new appuserprincipal(user); httpcontext.current.user = principal; } private static formsauthenticationticket getticketfromcurrentcookie() { var cookie = httpcontext.current.request.cookies[formsauthentication.formscookiename]; if (cookie == null) { global.writelog("getticketfromcurrentcookie", "cookie becomes null"); return null; } var ticket = formsauthentication.decrypt(cookie.value); return ticket; } static logindetails deserializeuserinfointernal(string name, string userdata) { var deserialize = new javascriptserializer(); var logindetails = deserialize.deserialize<logindetails>(userdata); return logindetails; }
Comments
Post a Comment