Layout 在布局页面上查看自定义用户数据的模型

Layout 在布局页面上查看自定义用户数据的模型,layout,model,asp.net-mvc-3,user-data,formsauthenticationticket,Layout,Model,Asp.net Mvc 3,User Data,Formsauthenticationticket,我的用户已通过身份验证,但在FormsAuthenticationTicket和cookie中存储了额外的信息 // Custom UserData will contain the following | seperated values: // [ FullName | ContactNumber1 | ContactNumber2 ] string userData = fullName + "|" + contactNumber1 + "|" + contactNumber2 Form

我的用户已通过身份验证,但在FormsAuthenticationTicket和cookie中存储了额外的信息

// Custom UserData will contain the following | seperated values:
// [ FullName | ContactNumber1 | ContactNumber2 ]
string userData = fullName + "|" + contactNumber1 + "|" + contactNumber2

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                                                1,
                                                userName,
                                                DateTime.Now,
                                                DateTime.Now.AddMinutes(20),
                                                false,
                                                userData,
                                                FormsAuthentication.FormsCookiePath);
创建Cookie等

这一切都很好

我正在使用MVC3和Razor

我有一个foo.Layout.cshtml,在页面顶部显示全名/联系人号码等

我可以在foo.Layout.cshtml的顶部包含代码,以提取显示以下值的代码:

@{

    FormsAuthenticationTicket ticket = ( (FormsIdentity)User.Identity ).Ticket;

    // Custom UserData will contain the following | seperated values:

    // [ FullName | ContactNumber1 | ContactNumber2 ]

    string[] userData = ticket.UserData.Split(new char[] { '|' });

    string fullName = userData[0];

    string contactNumber1 = userData[1];

    string contactNumber2 = userData[2];

}

<div>@fullName</div>

<div>@contactNumber1</div>

<div>@contactNumber2</div>
创建控制器

public class FooController : Controller {

    private readonly _userViewModel = null;

    public FooController( ) {

      // NOTE this would actually be behind an interface in some type of 
      //      repository / service pattern and injected but for the sake 
      //      of easy conversation ive put it here.

      FormsAuthenticationTicket ticket = ( (FormsIdentity)User.Identity ).Ticket;

      // Custom UserData will contain the following | seperated values:
      // [ FullName | ContactNumber1 | ContactNumber2 ]
      string[] userData = ticket.UserData.Split(new char[] { '|' });

      _userViewModel = new UserViewModel();

      userViewModel.FullName = userData[0];
      userViewModel.ContactNumber1 = userData[1];
      userViewModel.ContactNumber2 = userData[2];

    }

    public ActionResult Index() {

        (HOWEVER_SET_ON_LAYOUT_PAGE) = userViewModel.FullName;

        // If not posting ViewModel            
        (HOWEVER_SET_ON_THIS_PAGE) = userViewModel.FullName;

        // If posting ViewModel
        return View(_userViewModel);

    }
也许其中一些可以放在控制器工厂里

我只是想了解其他人是如何处理这种非常常见的设置的讨论或想法


我的实际任务是通过布局在每个页面的顶部显示用户的全名,并在正在填写然后提交的各种表单的底部显示全名和其他数据。回发后,将从cookie中重新读取全名,并发送到其他表单字段中存储。这有意义吗?真的是那个时候吗:-/

我在最近的项目中已经多次提到过这个问题

甚至比将其放入控制器更好的方法是创建一个身份验证服务,该服务包装您的表单身份验证特定代码,然后通过IoC在您可能需要的地方注入该服务。几乎所有IoC容器都应该能够指定每个http请求都可以缓存身份验证服务,因此您可以利用这一点来帮助节省资源/提高性能


现在,如果您需要在很多地方显示此用户信息,我建议您创建一个单独的UserController,并在视图中需要用户信息的任何位置对其进行渲染调用,而不是将您的用户视图模型作为一组其他视图模型的一部分,除非其他模型/视图实际需要它来满足某些业务条件,等等。。通过这种方式,您可以将用户信息作为一个组件提供一个标准的、规范的视图,并从中获得大量的重用。

您能提供一个实际的示例吗??
public class FooController : Controller {

    private readonly _userViewModel = null;

    public FooController( ) {

      // NOTE this would actually be behind an interface in some type of 
      //      repository / service pattern and injected but for the sake 
      //      of easy conversation ive put it here.

      FormsAuthenticationTicket ticket = ( (FormsIdentity)User.Identity ).Ticket;

      // Custom UserData will contain the following | seperated values:
      // [ FullName | ContactNumber1 | ContactNumber2 ]
      string[] userData = ticket.UserData.Split(new char[] { '|' });

      _userViewModel = new UserViewModel();

      userViewModel.FullName = userData[0];
      userViewModel.ContactNumber1 = userData[1];
      userViewModel.ContactNumber2 = userData[2];

    }

    public ActionResult Index() {

        (HOWEVER_SET_ON_LAYOUT_PAGE) = userViewModel.FullName;

        // If not posting ViewModel            
        (HOWEVER_SET_ON_THIS_PAGE) = userViewModel.FullName;

        // If posting ViewModel
        return View(_userViewModel);

    }