以编程方式更改FormsAuthenticationTicket中的用户数据 [英] Change the user data in FormsAuthenticationTicket programmatically

查看:70
本文介绍了以编程方式更改FormsAuthenticationTicket中的用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FormsAuthenticationTicket并放置数据并将数据跨所有页面传递. 如果我们不更改任何数据,它将起作用.

I am using the FormsAuthenticationTicket and place the data and passing the data across all the pages. and it will work if we are not changing any data.

所以,现在,如果我想更改数据并将其传递给cookie并进行加密,那么如何以编程方式更改数据.

So, now if I want to change the data and pass it for the cookie and encrypt then how to change the data programmatically.

请给我解决方案,以编程方式更改HttpCookie中的数据.

Please give me the solution for changing the data in HttpCookie programmatically.

推荐答案

这是我如何修改已发布的表单身份验证票证的示例:

This is an example of how I modify an already-issued forms auth ticket:

HttpCookie cookie = FormsAuthentication.GetAuthCookie(Username, true);
var ticket = FormsAuthentication.Decrypt(cookie.Value);

// Store UserData inside the Forms Ticket with all the attributes
// in sync with the web.config
var newticket = new FormsAuthenticationTicket(ticket.Version,
                                              ticket.Name,
                                              ticket.IssueDate,
                                              ticket.Expiration,
                                              true, // always persistent
                                              "User Data",
                                              ticket.CookiePath);

// Encrypt the ticket and store it in the cookie
cookie.Value = FormsAuthentication.Encrypt(newticket);
cookie.Expires = newticket.Expiration.AddHours(24);
this.Context.Response.Cookies.Set(cookie);

这篇关于以编程方式更改FormsAuthenticationTicket中的用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆