【问题标题】:Implementing IPrincipal and IIdentity in MVC with use of custom membership and role provider使用自定义成员资格和角色提供者在 MVC 中实现 IPrincipal 和 IIdentity
【发布时间】:2011-09-29 15:32:27
【问题描述】:

我一直坚持自定义 iprincpal 和 iidentity 对象的实现。我现在花了一天的时间来搜索如何实现这些权利并使用更多信息对其进行扩展。

我想使用自定义变量(如全名或其他内容)扩展信息 @Context.User.Identity.Name

编辑:现在我得到了以下代码,但如果我尝试读取 @((CustomPrincipal)Context.User.Identity).Nachname,我会收到一个错误,即无法将 System.Web.Security.FormsIdentity 转换为 CustomPrincipal

有什么想法吗?

public class CustomPrincipal : GenericPrincipal
{
    public CustomPrincipal(IIdentity identity, String[] roles) : base(identity, roles){ 

    }
    public String Vorname { get; set; }
    public String Nachname { get; set; } 
}

帐户模型:

public class FormsAuthenticationService : IFormsAuthenticationService
{
    public void SignIn(string userName, bool createPersistentCookie)
    {
        if (String.IsNullOrEmpty(userName)) throw new ArgumentException("Der Wert darf nicht NULL oder leer sein.", "userName");
        // Grab user information to insert
        KIMembershipUser membershipUser = (KIMembershipUser)Membership.GetUser(userName);
        var customInfo = String.Format("{0}|{1}", membershipUser.Vorname, membershipUser.Nachname);
        // Create and encrypt the ticket
        var ticket = new FormsAuthenticationTicket(
            2, // Version number
            userName, // Username
            DateTime.Now, // Issue date
            DateTime.Now.AddMinutes(30), // Expiration date
            createPersistentCookie, // Is it persistent?
            customInfo // User data
        );
        var encTicket = FormsAuthentication.Encrypt(ticket);
        // Store the ticket into a cookie
        var cookie = FormsAuthentication.GetAuthCookie(FormsAuthentication.FormsCookieName,createPersistentCookie);
        cookie.Value = encTicket;
        // Append the cookie to the response
        HttpContext.Current.Response.Cookies.Add(cookie); 

        //FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
    }

    public void SignOut()
    {
        FormsAuthentication.SignOut();
    }
}

global.asax:

    protected void Application_PostAuthenticateRequest(){
        // Collect current security information
        var principal = HttpContext.Current.User as RolePrincipal;
        if (principal == null)
            return;
        var identity = principal.Identity as FormsIdentity;
        if (identity == null)
            return;
        var roles = principal.GetRoles();
        // Extract user data in the authentication ticket
        var customInfo = identity.Ticket.UserData;
        var tokens = customInfo.Split('|');
        // Build a richer principal object
        var CustomPrincipal = new CustomPrincipal(identity, roles){
            Vorname = tokens[0],
            Nachname = tokens[1]
        };
        // Store the new principal in the HttpContext
        HttpContext.Current.User = CustomPrincipal;
    }

【问题讨论】:

  • 扩展 GenericPrincipal 和 GenericIdentity 类有什么问题?
  • 我不知道有这些对象。如何扩展它们?
  • 您可能不需要扩展(继承)它们,最简单的方法是为它们每个构造一个新实例:msdn.microsoft.com/en-us/library/y9dd5fx0(v=VS.100).aspx
  • @Islam Ibrahim 我用昨天的工作编辑了我的第一篇文章。也许你有一个想法?
  • 执行 (CustomPrincipal)Context.User).Nachname 而不是 (CustomPrincipal)Context.User.Identity).Nachname。

标签: c# model-view-controller iprincipal iidentity


【解决方案1】:

使用(CustomPrincipal)Context.User).Nachname 代替(CustomPrincipal)Context.User.Identity).Nachname

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    相关资源
    最近更新 更多