在MVC3中使用自定义IPrincipal和IIdentity

时间:2012-05-24 17:17:25

标签: asp.net-mvc-3 forms-authentication iprincipal iidentity custom-authentication

我创建了自己的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

1 个答案:

答案 0 :(得分:19)

你的错误在这里:

_application.AuthenticateRequest += ApplicationAuthenticateRequest;

HttpModule名为RoleManagerModule的{​​{1}}调用HttpApplication.PostAuthenticateRequest中的方法,并将HttpContext.Current.User设置为RolePrincipal。因此,您在User设置了AuthenticateRequestRoleManagerModule将其设置为PostAuthenticateRequest,意味着您设置后,因此会覆盖您的设置。更改您的Module.Init

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

重要更新:

如果这个问题不起作用,请再次查看this question - 由启动器决定,依赖当前问题 - 第二个解决方案。