员资格和角色提供者实施的IPrincipal和IIdentity

员资格和角色提供者实施的IPrincipal和IIdentity

本文介绍了在MVC与使用习惯成员资格和角色提供者实施的IPrincipal和IIdentity的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我坚持使用自定义iprincpal和的IIdentity对象的实现。我花了一天,现在搜索如何实现这些权利,并与更多的信息扩展它。



我要扩展信息 @ Context.User 。.Identity.Name 像全名或别的什么自定义变量



修改:现在,我得到了以下代码,但如果我尝试读 @((CustomPrincipal)Context.User.Identity).Nachname 我得到一个错误的System.Web。 Security.FormsIdentity 不能被强制转换为 CustomPrincipal



任何想法

 公共类CustomPrincipal:?的GenericPrincipal 
{
公共CustomPrincipal(的IIdentity身份的String []角色):基地(身份,角色){

}
公共字符串Vorname {搞定;组; }
公共字符串Nachname {搞定;组; }
}



AccountModel:

 公共类FormsAuthenticationService:IFormsAuthenticationService 
{
公共无效签到(用户名字符串,布尔createPersistentCookie)
{
如果(String.IsNullOrEmpty(用户名))抛出新的ArgumentException(username的)明镜DARF范沃特NULL nicht奥德盛媚眼。
//抓取用户信息插入
KIMembershipUser =的MembershipUser(KIMembershipUser)Membership.GetUser(用户名);
VAR customInfo =的String.Format({0} | {1},membershipUser.Vorname,membershipUser.Nachname);
//创建并加密门票
变种票=新的FormsAuthenticationTicket(
2,//版本号
用户名,用户名//
DateTime.Now,//发行日期
DateTime.Now.AddMinutes(30),//有效期
createPersistentCookie,//是否持续
customInfo //用户数据
)?;
VAR encTicket = FormsAuthentication.Encrypt(票);
//存放票到cookie
变种的cookie = FormsAuthentication.GetAuthCookie(FormsAuthentication.FormsCookieName,createPersistentCookie);
cookie.Value = encTicket;
//追加cookie来响应
HttpContext.Current.Response.Cookies.Add(饼干);

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

公共无效SignOut()
{
FormsAuthentication.SignOut();
}
}



Global.asax的:

 保护无效Application_PostAuthenticateRequest(){
//收集当前的安全信息
VAR本金= HttpContext.Current.User为RolePrincipal;
如果(本金== NULL)
的回报;
变种标识= principal.Identity为FormsIdentity;
如果(身份== NULL)
的回报;
变种角色= principal.GetRoles();在身份验证票证
VAR customInfo = identity.Ticket.UserData
//提取用户数据;
VAR令牌= customInfo.Split('|');
//建立一个更丰富的主体对象
变种CustomPrincipal =新CustomPrincipal(身份,角色){
Vorname =令牌[0],
Nachname =令牌[1]
};
//将在HttpContext的$新的主体b $ B HttpContext.Current.User = CustomPrincipal;
}


解决方案

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


I'm stuck with the implementation of a custom iprincpal and iidentity object. I spend a day now for searching how to implement these right and extend it with more informations.

I want to extend the Information @Context.User.Identity.Name with custom variables like full name or something else.

EDIT: Now i got the following code but if i try to read @((CustomPrincipal)Context.User.Identity).Nachname I'm getting a error that System.Web.Security.FormsIdentity could not be casted to CustomPrincipal.

Any ideas?

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

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

AccountModel:

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;
    }
解决方案

Use (CustomPrincipal)Context.User).Nachname instead of (CustomPrincipal)Context.User.Identity).Nachname

这篇关于在MVC与使用习惯成员资格和角色提供者实施的IPrincipal和IIdentity的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:21