【问题标题】:Generic class to identify Current Session information in ASP.Net MVC application用于识别 ASP.Net MVC 应用程序中的当前会话信息的通用类
【发布时间】:2019-02-01 18:22:10
【问题描述】:

我正在使用 ASP.Net MVC 开发一个简单的基于自定义角色的 Web 应用程序,在我的登录操作中,我正在创建一个 Profile 会话,如下所示:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
    using (HostingEnvironment.Impersonate())
    {
        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                var employeeProfile = AccountBal.Instance.GetEmployee(loginId);
                Session["Profile"] = employeeProfile;
                FormsAuthentication.SetAuthCookie(model.UserName, true);
            }
        }
        // If we got this far, something failed, redisplay form
        ModelState.AddModelError("", @"The user name or password provided is incorrect.");
        return View(model);
    }
}

我正在检查这个或在所有控制器操作中使用这个会话,如下所示:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateOrEdit(MyModel model)
{
    var employee = (Employee) Session["Profile"];
    if (employee == null) 
       return RedirectToAction("Login", "Account");

    if (ModelState.IsValid)
    {
        // Functionality goes here....
    }
}   

有什么方法可以将这段会话检查代码移动到基类或集中类中?因此,我不需要每次都在 Controller Actions 中检查它,而是直接访问属性

说,

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateOrEdit(MyModel model)
{
    var employee = _profileBase.GetCurrentProfile();
    if (employee == null) 
       return RedirectToAction("Login", "Account");

    if (ModelState.IsValid)
    {
        // Functionality goes here....
    }
}  

【问题讨论】:

  • 使用BaseController 继承您的控制器,并在该控制器中写入方法GetCurrentProfile() 并在其中添加您的会话代码GetCurrentProfile,并在您想要的控制器中使用此方法,如var employee = GetCurrentProfile();

标签: asp.net-mvc session model-view-controller base-class


【解决方案1】:

创建一个包含您的 GetCurrentProfile 方法的基本控制器来检索当前用户配置文件,例如

public class BaseController : Controller
{
    public Employee GetCurrentProfile()
    {
        return (Employee)Session["Profile"];
    }

    public bool SetCurrentProfile(Employee emp)
    {
        Session["Profile"] = emp;
        return true;
    }
}

并使用上面的BaseController 继承您想要的控制器并访问您的GetCurrentProfile 方法,如下所示

public class HomeController : BaseController
{
    public ActionResult SetProfile()
    {
        var emp = new Employee { ID = 1, Name = "Abc", Mobile = "123" };

        //Set your profile here
        if (SetCurrentProfile(emp))
        {
            //Do code after set employee profile
        }
        return View();
    }

    public ActionResult GetProfile()
    {
        //Get your profile here
        var employee = GetCurrentProfile();

        return View();
    }
}

GetCurrentProfileSetCurrentProfile 直接可用于您所需的控制器,因为我们直接从 BaseController 继承它。

你可以在上面的代码 sn-p 中使用try/catch

尝试一次可能对你有帮助

【讨论】:

  • @55SK55,查看答案可能对你有帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 2012-08-05
相关资源
最近更新 更多