如何以编程方式为单个应用程序提供多个身份验证Cookie

Les*_*nks 5 c# forms asp.net authentication web-config

我正在编写一个内容管理系统,用户可以在其中创建多个站点.每个站点都可以进行身份​​验 我试图找出如何为应用程序提供多个身份验证cookie,而无需将每个cookie添加到web.config.我需要在应用程序启动时以编程方式创建它们.这可能吗?

防爆.

SecureApp:http:// localhost/CTMS - 需要身份验证才能更新站点

CustomSite:http:// localhost/CTMS/Custom1 - 需要与SecureApp分开进行身份验证

希望这是有道理的.

Dun*_*ill 9

你可以这样做 -

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);
Run Code Online (Sandbox Code Playgroud)

然后你可以编写代码来检查传入的请求,看看他们是否有这个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
}
Run Code Online (Sandbox Code Playgroud)