MVC3 Windows身份验证覆盖User.Identity

Tob*_*nes 11 intranet windows-authentication windows-principal windows-identity asp.net-mvc-3

我正在使用带有MSSQL后端的MVC3 构建Intranet应用程序.我有身份验证和角色(通过自定义角色提供程序)正常工作.我现在要做的是重写User.Identity以允许像User.Identity.FirstName这样的项目.但我找不到任何代码可以告诉我在WindowsIdentity中如何做到这一点

我试过写一个自定义提供程序:

public class CPrincipal : WindowsPrincipal
{
    UserDAL userDAL = new UserDAL();
    public CPrincipal(WindowsIdentity identity)
        : base(identity)
    {
        userInfo = userDAL.GetUserProfile(identity.Name.Split('\\')[1]);
        this.identity = identity;
    }
    public UserInfo userInfo { get; private set; }
    public WindowsIdentity identity { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)

并覆盖WindowsAuthentication以填充自定义主体.

    void WindowsAuthentication_OnAuthenticate(object sender, WindowsAuthenticationEventArgs e)
    {
        if (e.Identity != null && e.Identity.IsAuthenticated)
        {
            CPrincipal cPrincipal = new CPrincipal(e.Identity);
            HttpContext.Current.User = cPrincipal;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我在身份验证功能中有一个断点,并且正在填充主体; 但是,当我在控制器中放置一个断点时,User只是它的正常RolePrincipal,而不是我的自定义主体.我究竟做错了什么?

编辑:

我在global.asax中注释掉了上面的代码.我使用C#覆盖了AuthorizeAttribute:

public class CAuthorize : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        bool authorized = base.AuthorizeCore(httpContext);
        if (!authorized)
        {
            return false;
        }


        IIdentity user = httpContext.User.Identity;
        CPrincipal cPrincipal = new CPrincipal(user);
        httpContext.User = cPrincipal;

        return true;
    } 

}
Run Code Online (Sandbox Code Playgroud)

并将我的校长调整为以下内容:

public class CPrincipal : IPrincipal
{
    private UserDAL userDAL = new UserDAL();
    public CPrincipal(IIdentity identity)
    {
        userInfo = userDAL.GetUserProfile(identity.Name.Split('\\')[1]);
        this.Identity = identity;
    }
    public UserInfo userInfo { get; private set; }

    public IIdentity Identity { get; private set; }

    public bool IsInRole(string role)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,当我放入断点时,手表会在用户中显示以下内容:

  • 用户
    • [CSupport.Model.CPrincipal]
    • 身分

身份是可访问的; 但是,它仍然是WindowsIdentity CPrincipal只能在手表中访问,不能直接访问.

编辑: 感谢所有为此做出贡献的人.您已经大大扩展了我对各个部分如何工作的理解.

我有两种工作方式,所以我想我会分享.

选项1:覆盖Global.asax中的授权请求

这是我要去的那个.

我没有使用Application_AuthenticateRequest,因为(根据这个:即使启用Windows身份验证,HttpContext.Current.User为null)用户还没有在Windows身份验证过程中填充,因此没有什么可以用来获取用户信息.

Application_AuthorizeRequest是链中的下一个,在引入Windows标识后发生.

    protected void Application_AuthorizeRequest(object sender, EventArgs e)
    {
        if (User.Identity.IsAuthenticated && Roles.Enabled)
        {
            Context.User = new FBPrincipal(HttpContext.Current.User.Identity);
        }
    }
Run Code Online (Sandbox Code Playgroud)

这是Principal的覆盖

public class CPrincipal : IPrincipal
{
    private UserDAL userDAL = new UserDAL();
    public CPrincipal(IIdentity identity)
    {
        userInfo = userDAL.GetUserProfile(identity.Name.Split('\\')[1]);
        this.Identity = identity;
    }
    public UserInfo userInfo { get; private set; }

    public IIdentity Identity { get; private set; }

    public bool IsInRole(string role)
    {
        return userDAL.IsUserInRole(userInfo.UserName, role);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是您在创建的新Principal中访问更新信息的方法.

    [Authorize(Roles = "super admin")]
    public ActionResult Dashboard()
    {
        string firstname = (User as CPrincipal).userInfo.FirstName; // <--
        DashboardModel dModel = reportDAL.GetChartData();
        return View(dModel);
    }
Run Code Online (Sandbox Code Playgroud)

选项2:覆盖AuthorizeAttribute

这是被覆盖的校长(与上面相同)

public class CPrincipal : IPrincipal
{
    private UserDAL userDAL = new UserDAL();
    public CPrincipal(IIdentity identity)
    {
        userInfo = userDAL.GetUserProfile(identity.Name.Split('\\')[1]);
        this.Identity = identity;
    }
    public UserInfo userInfo { get; private set; }

    public IIdentity Identity { get; private set; }

    public bool IsInRole(string role)
    {
        return userDAL.IsUserInRole(userInfo.UserName, role);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是授权属性的覆盖

public class CAuthorize : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        bool authorized = base.AuthorizeCore(httpContext);
        if (!authorized)
        {
            return false;
        }


        IIdentity user = httpContext.User.Identity;
        CPrincipal cPrincipal = new CPrincipal(user);
        httpContext.User = cPrincipal;

        return true;
    } 

}
Run Code Online (Sandbox Code Playgroud)

您可以在此处更改要使用的AuthorizeAttribute并使用新信息.

    [CAuthorize(Roles = "super admin")] // <--
    public ActionResult Dashboard()
    {
        string firstname = (User as CPrincipal).userInfo.FirstName; // <--
        DashboardModel dModel = reportDAL.GetChartData();
        return View(dModel);
    }
Run Code Online (Sandbox Code Playgroud)

选项1处理全局覆盖,选项2处理单个级别的所有内容.

Eri*_*sch 6

你不应该这样做,而应该覆盖global.asax中的Application_AuthenticateRequest方法,然后使用Current.User而不是HttpContext.Current.User(不知道为什么,但是有区别).

那么,在控制器中访问它的一种简单方法是创建一个扩展方法?像这样的东西:

public static class IIdentityExtensions {
    public static IMyIdentity MyIdentity(this IIdentity identity) {
        return (IMyIdentity)identity;
    }
}
Run Code Online (Sandbox Code Playgroud)

那么你可以说User.Identity.IMyIdenty().FirstName.您也许可以将此作为财产.

这是我使用的代码:

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    FormsAuthenticationTicket authTicket = FormsAuthentication
       .Decrypt(authCookie.Value);
    var identity = new MyIdentity(authTicket.Name, "Forms", 
       FormsAuthenticationHelper.RetrieveAuthUserData(authTicket.UserData));
    Context.User = new GenericPrincipal(identity, 
       DependencyResolver.Current.GetService<ISecurityHandler>()
          .GetRoles(identity.Name).ToArray());
}
Run Code Online (Sandbox Code Playgroud)

现在,忽略DependencyResolver的东西和自定义身份验证票据,这是非常基本的,对我来说正常.

然后,在我的应用程序中,当我需要来自我的自定义身份的信息时,我只((IMyIdentity)User.Identity).FirstName需要使用或者我需要的任何内容.这不是火箭科学,而是有效的.