Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i'm creating an application that have unity framework for dependency injection so my Account Controller is following and that contain Login Action


C#
public class AccountController : Controller
    {

        private readonly IUserService userService;

        public AccountController(IUserService userService)
        {
            this.userService = userService;
        }
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        //[ValidateAntiForgeryToken]
        public ActionResult Login(LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                string message = "";
                var user = userService.UserExists(model, out message);
                
                if (user != null)
                {
                    CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();
                    serializeModel.UserId = Convert.ToInt32(user.Id);
                    serializeModel.EmailId = user.EmailId;
                    serializeModel.FirstName = user.FirstName;
                    serializeModel.LastName = user.LastName;
                    serializeModel.role = user.Role;

                    string userData = JsonConvert.SerializeObject(serializeModel,Formatting.Indented,
                        new JsonSerializerSettings
                        {
                            PreserveReferencesHandling = PreserveReferencesHandling.Objects
                        });
                    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                             1,
                            user.EmailId,
                             DateTime.Now,
                             model.RememberMe ? DateTime.Now.AddDays(3) : DateTime.Now.AddHours(3),
                             model.RememberMe ? true : false,
                             userData);
                    
                    string encTicket = FormsAuthentication.Encrypt(authTicket);
                    HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                    faCookie.Expires = model.RememberMe ? DateTime.Now.AddDays(3) : DateTime.Now.AddHours(3);                    
                    Response.Cookies.Add(faCookie);                    
                    return RedirectToAction("Index", "Home");                    
                }

            }
            else
            {
                ModelState.AddModelError("", "Incorrect username and/or password");
            }
            return View(model);
        }
}



.in login action i'm doing following
1) Checking User Is Existed Or Not if existed then adding FormsAuthentication Cookie .
but when i using IUserService (Interface for DI ) then cookie is deleted when redirect to another action .
2) But If I remove DI from controller then Cookie Is Persisted .

whats wrong i'm finding answer from couple of days .
please give me solution i want DI and Cookie Both .

Thanks in advanced .
Posted

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