używanie niestandardowych IPrincipal i IIdentity w MVC3
Tworzę własną implementację IPrincipal
i IIdentity
Jak pokazano poniżej:
[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; }
}
}
Ponadto tworzę HttpModule
i w jego AuthenticateRequest
zdarzeniu robię to:
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;
}
Również tworzę swoje własne Controller
i WebViewPage
jak te:
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;
}
}
}
Jak pokazano w powyższym kodzie, wydaje się, że wszystko jest w porządku; ale jak widzisz, wmiejsce numer 1 nie mogę uzyskać dostępu do CustomPrincipal
! Oznacza to, że w tym miejscu mam RolePrincipal
zamiast CustomPrincipal
. np. HttpContext.Current.User
jest RolePrincipal
zamiast CustomPrincipal
. Ale RolePrincipal.Identity
nieruchomość jest CustomIdentity
!
1 answers
Twój błąd jest tutaj:
_application.AuthenticateRequest += ApplicationAuthenticateRequest;
Istnieje HttpModule
o nazwie RoleManagerModule
, która wywołuje metodę w HttpApplication.PostAuthenticateRequest
i ustawia HttpContext.Current.User
na RolePrincipal
. Więc Ustawiłeś User
w AuthenticateRequest
i RoleManagerModule
ustawia go w PostAuthenticateRequest
, czyli po ustawieniu, więc nadpisuje Twoje ustawienia. Zmień swój Module.Init
:
public void Init(HttpApplication context) {
_application = context;
// change just this line:
_application.PostAuthenticateRequest += ApplicationAuthenticateRequest;
}
WAŻNA AKTUALIZACJA:
Proszę zobaczyć to pytanie -zadawane przez starter ponownie, zależne od bieżącego pytania - dla drugiego rozwiązania, jeśli to nie działa.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:00:27