17

tworzę własną IPrincipal i IIdentity realizacji, jak pokazano poniżej:użyciu niestandardowego IPrincipal i IIdentity w 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; } 
    } 

} 

Również tworzę HttpModule iw jego AuthenticateRequest razie zrobić 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; 
    } 

Ponadto tworzę własne Controller i WebViewPage takie jak:

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 widać, w Miejsce numer 1 Nie mogę uzyskać dostępu do CustomPrincipal! Znaczy w tym miejscu, mam RolePrincipal zamiast mieć CustomPrincipal. na przykład HttpContext.Current.User to RolePrincipal zamiast CustomPrincipal. Ale właściwość RolePrincipal.Identity to CustomIdentity!

Odpowiedz

19

Twój błąd jest tutaj:

_application.AuthenticateRequest += ApplicationAuthenticateRequest; 

Jest HttpModule nazwie RoleManagerModule który wywołuje metodę w HttpApplication.PostAuthenticateRequest i ustawia HttpContext.Current.User do RolePrincipal. Tak więc ustawiłeś User w AuthenticateRequest, a RoleManagerModule ustawia go w PostAuthenticateRequest, czyli po twoim zestawie, więc nadpisuje twoje ustawienia. Zmienić Module.Init:

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

WAŻNE UPDATE:

proszę zobaczyć this question -asked przez rozrusznik ponownie, zależy od aktualnej wątpliwa dla drugiego rozwiązania, jeśli ten nie działa.

+0

To działa! Wielkie dzięki. –

+0

Dzięki za tę odpowiedź; Wyciągałam włosy, próbując to wymyślić! –

+0

@DavidKeaveny Dziękuję, ale muszę powiedzieć, że rozwiązanie ma jeszcze problem. Wyszukaj w pytaniach 'king.net', możesz znaleźć Q powiązane na tym, że jest inny problem z usługami IIS, więc sugeruję sposób, w jaki on/ona rozwiązuje problem. Proszę spojrzeć na pytania 'king.net', znajdziesz to. Pozdrowienia. –

Powiązane problemy