c# - How do I add the user ID To authenticate a cookie in mvc4 -
how add user id ti authenticated cookie
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; using system.web.security; using system.security.cryptography; using system.text; namespace project.controllers { public class logincontroller : controller { // get: /login/ private datacontext db = new datacontext(); public actionresult index() { return view(); } [httppost] public actionresult index(loginmodel model) { if (modelstate.isvalid) { string username = model.username; user login = db.users.where(m => m.name == username).first(); string pass = convert.tobase64string(new md5cryptoserviceprovider().computehash(new utf8encoding().getbytes(model.password))); if (login != null && pass == login.password) { formsauthentication.setauthcookie(model.username, false); return redirecttoaction("index", "project"); } modelstate.addmodelerror("", "invalid username or password"); } return view(); } } }
this code project article explains in pretty detail different ways accomplish trying do. http://www.codeproject.com/articles/36836/forms-authentication-and-role-based-authorization
if (!formsauthentication.cookiessupported) { //if authentication ticket specified not use cookie, set in uri formsauthentication.setauthcookie(encrypetedticket, createpersistentcookie); } else { //if authentication ticket specified use cookie, wrap within cookie. //the default cookie name .aspxauth if not specified //in <forms> element in web.config httpcookie authcookie = new httpcookie(formsauthentication.formscookiename, encrypetedticket); //set cookie's expiration time tickets expiration time if(ticket.ispersistent) authcookie.expires =ticket.expiration ; ////set cookie in response httpcontext.current.response.cookies.add(authcookie); }
Comments
Post a Comment