Использование пользовательских IPrincipal и IIdentity в MVC3

Я создаю свою собственную реализацию IPrincipal и IIdentity, как показано ниже:

[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;
    }

Кроме того, я создаю свои собственные Controller и WebViewPage следующим образом:

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.User является RolePrincipal вместо CustomPrincipal. Но свойство RolePrincipal.Identity является CustomIdentity!

Ответ 1

Ваша ошибка здесь:

_application.AuthenticateRequest += ApplicationAuthenticateRequest;

Существует HttpModule с именем RoleManagerModule, который вызывает метод в HttpApplication.PostAuthenticateRequest и устанавливает HttpContext.Current.User в RolePrincipal. Итак, вы устанавливаете User в AuthenticateRequest, а RoleManagerModule устанавливает его в PostAuthenticateRequest, значит, после вашего набора, так что переопределяет ваши настройки. Измените Module.Init:

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

ВАЖНОЕ ОБНОВЛЕНИЕ:

См. этот вопрос, который снова задан стартером, зависит от текущего вопроса - для второго решения, если этот не работает.