ASP.NET MVC - 使用Windows身份验证的自定义IIdentity或IPrincipal

时间:2017-05-15 15:29:47

标签: asp.net-mvc windows-authentication extend iprincipal

我正在使用Windows身份验证的Intranet站点进行登录。但是,我想扩展IPrincipal以获得其他属性。例如,我想在@User.FirstNameUser.AuthorizedActivity("Admin/Permissions/Edit")中获取用户的FirstName(将从db检索)使用活动而不是角色隐藏某些链接等。我真的很想知道时间在过去的2天内搞清楚了,并通过Windows身份验证找到了很多信息。

我的CustomPrincipal和BaseViewPage设置:

namespace Intranet_v2.Helpers
{ 
    public interface ICustomPrincipal : IPrincipal
    {
        Guid UserGuid { get; set; }
        string FirstName { get; set; }
        string LastName { get; set; }
        string FullName { get; set; }
    }

    public class CustomPrincipal : ICustomPrincipal
    {
        public IIdentity Identity { get; private set; }
        public bool IsInRole(string role) { return false; }

        public CustomPrincipal(string identity)
        {
            this.Identity = new GenericIdentity(identity);
        }

        public Guid UserGuid { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName { get; set; }
    }

    public class CustomPrincipalSerializeModel
    {
        public Guid UserGuid { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName { get; set; }
    }

    public class BaseController : Controller
    {
        protected virtual new CustomPrincipal User
    {   
            get { return HttpContext.User as CustomPrincipal; }
        }
    }

    public abstract class BaseViewPage : WebViewPage
    {
        public virtual new CustomPrincipal User
        {
            get { return base.User as CustomPrincipal; }
        }
    }

    public abstract class BaseViewPage<TModel> : WebViewPage<TModel>
    {
        public virtual new CustomPrincipal User
        {
            get { return base.User as CustomPrincipal; }
        }
    }
}

观看Web.Config BaseViewPage:

<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="Intranet_v2.Helpers.BaseViewPage">
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Optimization"/>
    <add namespace="System.Web.Routing" />
    <add namespace="Intranet_v2" />
  </namespaces>
</pages>

我认为我的主要问题是我不知道在我的protected void Application_PostAuthenticateRequest(object sender, EventArgs args)文件的Global.asax.cs中该怎么做。我在这里设置它的尝试很差:

protected void Application_PostAuthenticateRequest(object sender, EventArgs args)
{
    //var application = (HttpApplication)sender;
    var context = application.Context;

    if (context.User != null || !context.User.Identity.IsAuthenticated) return;

    var formsIdentity = (FormsIdentity)context.User.Identity;

    if (formsIdentity == null) return;

    var ticket = formsIdentity.Ticket;


    JavaScriptSerializer serializer = new JavaScriptSerializer();

    CustomPrincipalSerializeModel serializeModel = serializer.Deserialize<CustomPrincipalSerializeModel>(ticket.UserData);

    CustomPrincipal newUser = new CustomPrincipal(ticket.Name);
    newUser.UserGuid = serializeModel.UserGuid;
    newUser.FirstName = serializeModel.FirstName;
    newUser.LastName = serializeModel.LastName;
    newUser.FullName = serializeModel.FullName;

    var values = ticket.UserData.Split('|');

    var roles = values[1].Split(',');

    context.User = new GenericPrincipal(new GenericIdentity(ticket.Name, "Forms"), roles);
}

现在我正处于@ User.Name现在为空的位置。我在这方面已经超出了我的想法。任何帮助表示赞赏。我的protected void Application_PostAuthenticateRequest(object sender, EventArgs args)完全没有了。

我想要做的就是依靠Windows身份验证来执行正常操作,并为HttpContext.Current.User添加一些额外的属性。任何帮助都表示赞赏......我不能成为唯一一个尝试这样做的人。

1 个答案:

答案 0 :(得分:1)

我通常做的只是稍后请求其他用户信息。例如,使用扩展方法,如:

4

这大致是我在一些项目中实施的;而不是点击登录过程并将其存储在cookie中,系统可以在需要信息时延迟加载信息并缓存在会话中。