本文介绍了在MVC Controller中使用Unity的DI正在重定向后删除Cookies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在创建一个具有依赖注入统一框架的应用程序,因此我的帐户控制器正在关注并包含登录操作





  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
用户。 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( 用户名和/或密码不正确);
}
返回查看(模型);
}
}







。登录操作我正在关注

1)检查用户是否存在如果存在然后添加FormsAuthentication Cookie。

但是当我使用IUserService(DI接口)时,当重定向到另一个动作时cookie被删除。

2)但如果我从控制器中删除DI,那么Cookie就会被保留。



我错了我几天都找到答案。

请给我解决方案我想要DI和Cookie两者。



先谢谢你。

解决方案

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]
        //[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 .

解决方案

这篇关于在MVC Controller中使用Unity的DI正在重定向后删除Cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 10:26