使用自定义的IPrincipal和IIdentity的在MVC3(using custom IPri

2019-06-24 18:08发布

创建我自己IPrincipalIIdentity实现如下图所示:

[ComVisible(true)]
[Serializable]
public sealed class CustomIdentity : IIdentity {

    private readonly string _name;
    private readonly string _email;
    // and other stuffs

    public CustomIdentity(string name) {
        _name = name.Trim();
        if(string.IsNullOrWhiteSpace(name))
            return;
        _email = (connect to database and read email and other stuffs);
    }

    public string Name {
        get { return _name; }
    }

    public string Email {
        get { return _email; }
    }

    public string AuthenticationType {
        get { return "CustomIdentity"; }
    }

    public bool IsAuthenticated {
        get { return !string.IsNullOrWhiteSpace(_name); }
    }

}


[ComVisible(true)]
[Serializable]
public sealed class CustomPrincipal : IPrincipal {

    private readonly CustomIdentity _identity;

    public CustomPrincipal(CustomIdentity identity) {
        _identity = identity;
    }

    public bool IsInRole(string role) {
        return _identity != null && 
               _identity.IsAuthenticated &&
               !string.IsNullOrWhiteSpace(role) &&
               Roles.IsUserInRole(_identity.Name, role);
    }

    IIdentity IPrincipal.Identity {
        get { return _identity; }
    }

    public CustomIdentity Identity {
        get { return _identity; }
    }

}

另外,我创建了一个HttpModule ,并在其AuthenticateRequest事件中,我这样做:

    public void Init(HttpApplication context) {
        _application = context;
        _application.AuthenticateRequest += ApplicationAuthenticateRequest;
    }

    private void ApplicationAuthenticateRequest(object sender, EventArgs e) {
        var formsCookie = _application.Request.Cookies[FormsAuthentication.FormsCookieName];
        var identity = formsCookie != null
             ? new CustomIdentity(FormsAuthentication.Decrypt(formsCookie.Value).Name)
             : new CustomIdentity(string.Empty);
        var principal = new CustomPrincipal(identity);
        _application.Context.User = Thread.CurrentPrincipal = principal;
    }

另外,我创造我自己的ControllerWebViewPage这样的:

public abstract class CustomController : Controller {
    public new CustomPrincipal User {
        get {
            var user = System.Web.HttpContext.Current.User as CustomPrincipal;
            return user;
        }
    }
}


public abstract class CustomWebViewPage<TModel> : WebViewPage<TModel> {
    public new CustomPrincipal User {
        get {
            // (Place number 1) here is the error I'm speaking about!!!
            var user = HttpContext.Current.User as CustomPrincipal;
            return user;
        }
    }
}

如上面的代码,似乎一切是正确的; 但是你可以看到,在广场1号我无法访问CustomPrincipal ! 意味着在这个地方,我有一个RolePrincipal而不是一个的CustomPrincipal 。 例如HttpContext.Current.UserRolePrincipal而不是CustomPrincipal 。 但RolePrincipal.Identity属性是CustomIdentity

Answer 1:

你的错误是在这里:

_application.AuthenticateRequest += ApplicationAuthenticateRequest;

有一个HttpModule命名RoleManagerModule调用的方法HttpApplication.PostAuthenticateRequest并将HttpContext.Current.UserRolePrincipal 。 所以,你设置的UserAuthenticateRequestRoleManagerModule其设置在PostAuthenticateRequest ,你的一套装置之后,那么将覆盖您的设置。 更改Module.Init

public void Init(HttpApplication context) {
    _application = context;
    // change just this line:
    _application.PostAuthenticateRequest += ApplicationAuthenticateRequest;
}

重要更新:

请看这个问题在起始-asked再次,取决于当前question-的第二个解决方案,如果这一个不工作。



文章来源: using custom IPrincipal and IIdentity in MVC3