开发者

.NET FormsAuthentication Generic Principal from Authentication Ticket

开发者 https://www.devze.com 2023-02-14 21:50 出处:网络
Does anyo开发者_运维百科ne know how to add a generic principal to the HTTPContext from the Forms Authentication Ticket?You could do this in the Application_AuthenticateRequest event. Here\'s an exampl

Does anyo开发者_运维百科ne know how to add a generic principal to the HTTPContext from the Forms Authentication Ticket?


You could do this in the Application_AuthenticateRequest event. 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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号