c# - 使用自定义成员资格和角色提供者在 MVC 中实现 IPrincipal 和 IIdentity

标签 c# model-view-controller iprincipal iidentity

我坚持执行自定义 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();
    }
}

全局.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;
    }

最佳答案

使用 (CustomPrincipal)Context.User).Nachname 而不是 (CustomPrincipal)Context.User.Identity).Nachname

关于c# - 使用自定义成员资格和角色提供者在 MVC 中实现 IPrincipal 和 IIdentity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6597243/

相关文章:

c# - Visual Studio 2012 找不到模板

iphone - 如果你有很多静态数据,如何遵循 MVC?

c# - 单元测试,如何设置Thread.CurrentPrincipal和IsAuthenticated

.net-4.5 - 您如何在可移植类库中使用 IPrincipal 和 IIdentity?

c# - 在 ASP.NET MVC 中使用表单例份验证的自定义 IPrincipal

c# - 没有要求参数的 MVC 4 JSON 路由发布数据路由

c# - 如何在 Windows.Forms.PictureBox 中正确显示 Kinect 视频流?

c# - "CancellationTokenSource"获取哈希码

ios - Swift - 求和函数错误

mysql - Rails belongs_to 和 has_many 没有创建主外键关系