hi
i'm creating an application that have unity framework for dependency injection so my Account Controller is following and that contain Login Action
public class AccountController : Controller
{
private readonly IUserService userService;
public AccountController(IUserService userService)
{
this.userService = userService;
}
public ActionResult Login()
{
return View();
}
[HttpPost]
[AllowAnonymous]
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 .