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

发布于 2024-10-17 04:47:56 字数 3120 浏览 2 评论 0原文

我的用户已通过身份验证,但在 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 等

所有这些都工作正常。

我使用 mvc 3 和 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 UserViewModel {

    public string FullName { get; set; }

    public string ContactNumber1 { get; set; }

    public string ContactNumber2 { get; set; }

}

创建控制器

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 中重新读取全名,并将其发送到与其他表单字段一起存储。这有意义吗?真的是那个时候吗:-/

My user is Authenticated but has extra information stored in a FormsAuthenticationTicket then a 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);

Create Cookie etc etc

All this is working fine.

Im using mvc 3 and Razor

I have a foo.Layout.cshtml that displays the Fullname / ContactNumber etc etc at the top of the page.

I could include code at the top of the foo.Layout.cshtml to extract an display these values:

@{

    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>

This means I dont have to include this code in my controllers but to be honest I dont like it and in my actual app (different values) I need the values in the majority of my views including the layout.

So I have debated including the code inside my controller:

Create a viewmodel:

public class UserViewModel {

    public string FullName { get; set; }

    public string ContactNumber1 { get; set; }

    public string ContactNumber2 { get; set; }

}

Create Controller

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);

    }

Maybe some of this could be put inside the controller factory.

Im just looking for discussion or ideas of how other people are approaching this quite common setup.

My actual task is to display the Users Full Name at the top of each page via the Layout and display the Full Name (and other data) at the bottom of various forms that are being filled in then submitted. After postback the Full Name will then be re read from cookie and sent to be stored with the other form fields. Does that make sense and is it really that time :-/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再见 2024-10-24 04:47:56

我在最近的项目中已经多次讨论过这个问题。

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

现在,如果您需要在很多地方显示此用户信息,我鼓励您创建一个单独的 UserController 并在视图中需要用户信息的任何地方对其进行 RenderAction 调用,而不是使您的用户视图模型成为一堆其他视图模型(除非其他模型/视图实际上需要它来满足某些业务条件等)。通过这种方式,您可以将用户信息作为组件获得标准的“规范”视图,并且可以从中获得大量重用。

I've addressed this exact thing a couple times in recent projects.

Even better than putting this in the controller would be to create an Authentication service that wraps your FormsAuthentication-specific code, then inject that service via IoC where ever you may need it. Pretty much any IoC container should be able to also specify that your Authentication service could be cached per-http request, so you can take advantage of that to help conserve resources / enhance performance.

Now if you need to display this user info in lots of places, I would encourage you to create a separate UserController and do a RenderAction call to it wherever the User info is needed in your views, instead of making your user view model a part of a bunch of other view models (unless it's actually needed by the other models/views to satisfy some business conditions, etc.). This way you can have a standard, "canonical" view of the user info, as a component, and you'll get lots of reuse out of it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文