从身份验证票证.NET FormsAuthentication通用校长 [英] .NET FormsAuthentication Generic Principal from Authentication Ticket

查看:125
本文介绍了从身份验证票证.NET FormsAuthentication通用校长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何将一个通用的主体从表单添加到HttpContext的身份验证票证?

Does anyone know how to add a generic principal to the HTTPContext from the Forms Authentication Ticket?

推荐答案

您可以在 Application_AuthenticateRequest 事件做到这一点。 下面是一个例子的:

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);
}

这篇关于从身份验证票证.NET FormsAuthentication通用校长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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