.NET FormsAuthentication来自身份验证票证的通用主体

时间:2011-03-02 16:52:42

标签: c# asp.net

有没有人知道如何从Forms Authentication Ticket向HTTPContext添加通用主体?

1 个答案:

答案 0 :(得分:3)

您可以在Application_AuthenticateRequest事件中执行此操作。 Here's an example

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    var user = HttpContext.Current.User;
    if (user == null || !user.Identity.IsAuthenticated)
    {
        return;
    }
    // read the roles from the cookie and set a custom generic principal
    var fi = (FormsIdentity) HttpContext.Current.User.Identity;
    var fat = fi.Ticket;
    var astrRoles = fat.UserData.Split('|');
    HttpContext.Current.User = new GenericPrincipal(fi, astrRoles);
}