我正在编写一个内容管理系统,用户可以在该系统内创建多个网站。每个站点都可以进行身份验证。我想弄清楚如何为应用程序设置多个身份验证 cookie,而不必将每个 cookie 添加到 web.config。我需要在应用程序启动时以编程方式创建它们。这可能吗?
例如
安全应用:http://localhost/CTMS - 需要身份验证才能更新站点
自定义站点:http://localhost/CTMS/Custom1 - 需要独立于 SecureApp 的身份验证
希望这是有道理的。
最佳答案
你可以这样做 -
FormsAuthenticationTicket _ticket = new FormsAuthenticationTicket(_version, _name, _issueDate, _expirationDate, _isPersistent, _userData, _cookiePath);
string _encryptedTicket = FormsAuthentication.Encrypt(_ticket);
HttpCookie _cookie = new HttpCookie("customticket", _encryptedTicket);
HttpContext.Current.Response.Cookies.Add(_cookie);
然后你可以编写代码来检查传入的请求,看看他们是否有这个 cookie -
HttpCookie _cookie = HttpContext.Current.Request.Cookies["customticket"];
if(_cookie){
_encryptedTicket = _cookie.Value;
FormsAuthenticationTicket _ticket = FormsAuthentication.Decrypt(_encryptedTicket);
if(!_ticket.Expired) {
IIdentity _identity = new FormsIdentity(_ticket);
IPrincipal _principal = new GenericPrincipal(_identity, new string[0]); //Identity plus string of roles.
}
}
else{
//dostuff
}
关于c# - 如何以编程方式为单个应用程序设置多个身份验证 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9813146/