我正在从URL中检索租户名称。我希望只执行一次,将其存储在cookie中,并在需要新页面请求时从那里检索它。

我正在使用下面的代码“创建” cookie。我希望该界面允许我存储其他信息,但事实并非如此。有没有办法做到这一点,或者我走错了路?

    public void SignIn(string userName, bool createPersistentCookie)
    {
        if (String.IsNullOrEmpty(userName))
            throw new ArgumentException("Value cannot be null or empty.", "userName");

        FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
    }

提前致谢。

最佳答案

就个人而言,我不会尝试更改Auth Cookie。而是创建一个新的cookie:

var myCookie = new HttpCookie("myCookie");//instantiate an new cookie and give it a name
myCookie.Values.Add("TenantName", "myTenantName");//populate it with key, value pairs
Response.Cookies.Add(myCookie);//add it to the client

然后,您可以像这样读取写入Cookie的值
var cookie = Request.Cookies["myCookie"];
var tenantName = cookie.Values["TenantName"].ToString();
//tenantName = "myTenantName"

09-28 11:28