Creo il mio propria implementazione IPrincipal
e IIdentity
come illustrato di seguito:utilizzando personalizzati IPrincipal e IIdentity in MVC3
[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; }
}
}
Inoltre, creo un HttpModule
e nella sua manifestazione AuthenticateRequest
, faccio questo:
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;
}
Inoltre, ho creare la mia Controller
e WebViewPage
come questi:
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;
}
}
}
come mostrato nel codice precedente, sembra che tutto sia corretto; Ma come puoi vedere, in Numero posto 1 Non riesco ad accedere allo CustomPrincipal
! Significa in questo posto, ho un RolePrincipal
invece di avere un CustomPrincipal
. per esempio. HttpContext.Current.User
è un RolePrincipal
anziché CustomPrincipal
. Ma la proprietà RolePrincipal.Identity
è una CustomIdentity
!
Funziona! Molte grazie. –
Grazie per questa risposta; Mi stavo strappando i capelli cercando di immaginarlo! –
@DavidKeaveny Grazie, ma devo dirvi che la soluzione ha ancora un problema. Cerca nelle domande di 'king.net', puoi trovare una Q associata su questo che c'è un altro problema con IIS, quindi ti suggerisco un modo in cui lui/lei, che risolve il problema. Per favore guarda le domande di 'king.net', lo troverai. Saluti. –