简体   繁体   English

一次存储和重用价值的最佳方法是什么

[英]What the best way to store value one time and reuse it

In my default page, I take the user name and role, and I want to reuse it in all other pages. 在默认页面中,我使用用户名和角色,并希望在所有其他页面中重用它。 What the best practice to access this user name and role in every page. 在每个页面中访问此用户名和角色的最佳实践是什么。 Is seesion the only solution for this or there is another way? 见解是解决此问题的唯一方法,还是有另一种方法?

You can either store that information in a Session or a Cookie . 您可以将该信息存储在SessionCookie中 Both are served to store user-information data and have their pros and cons. 两者均用于存储用户信息数据,各有利弊。

To read more about sessions & cookies, use the following links: 要阅读有关会话和cookie的更多信息,请使用以下链接:

https://msdn.microsoft.com/en-us/library/ms178581.aspx https://msdn.microsoft.com/zh-CN/library/ms178581.aspx

https://msdn.microsoft.com/en-us/library/ms178194.aspx https://msdn.microsoft.com/zh-CN/library/ms178194.aspx

As you need user name and roles It will be already stored in some of the variables. 当您需要用户名和角色时,它将已经存储在某些变量中。 Like User.Identity in MVC application. 就像MVC应用程序中的User.Identity。 I would suggest to use already available data instead of storing it again. 我建议使用已经可用的数据,而不是再次存储。

Yes, one good solution exists. 是的,存在一个好的解决方案。

You can make one class with getter and setter method of username and role within same namespace. 您可以使用同一名称空间中的用户名和角色的getter和setter方法创建一个类。 ex. 例如

public class UserData
{
    public string UserName{ set; get; }
    public string Role { set; get; }
}

and now you can get and set value of both(username and role). 现在您可以获取并设置两者的值(用户名和角色)。 like, 喜欢,

//Set both properties
UserData objUserData = new UserData();
objUserData.UserName = "set_user_name_you_want";
objUserData.Role = "set_role_you_want";

Now you can get that both properties anywhere you want within that same namespace. 现在,您可以在同一名称空间中的任何位置获得这两个属性。

//Get both properties
UserData objUserData = new UserData();
String UserName = objUserData.UserName;
String Role = objUserData.Role;

I hope this will helpful to you! 希望对您有所帮助! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM