2010-05-04 19 views
6

Zastanawiam się, jaki jest najlepszy sposób na zastąpienie genericPrincipal moim własnym CustomGenericPrincipal.Asp.net: Zastąp GenericPrincipal

W tej chwili mam coś takiego, ale nie jestem pewien, czy to prawda.

protected void Application_AuthenticateRequest(Object sender, EventArgs e) 
{ 
    HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; 
    if (authCookie != null) 
    { 
     FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); 
     var identity = new CustomIdentity(authTicket); 
     var principal = new CustomPrincipal(identity); 

     Context.User = principal; 
    } 
    else 
    { 
     //Todo: check if this is correct 
     var genericIdentity = new CustomGenericIdentity(); 
     Context.User = new CustomPrincipal(genericIdentity); 
    } 
} 

muszę wymienić, bo muszę Principal, która implementuje interfejs ICustomPrincipal moje, bo jestem w następujący sposób z Ninject:

Bind<ICustomPrincipal>().ToMethod(x => (ICustomPrincipal)HttpContext.Current.User) 
         .InRequestScope(); 

Więc co jest najlepszym sposobem na zastąpienie GenericPrincipal?

Dzięki z góry,

Pickels

Odpowiedz

5

Brakuje subtelny szczegół, gwint.

Context.User = Thread.CurrentPrincipal = new CustomPrincipal.... 

Dostaniesz się tam, gdzie chcesz się udać.

Zauważam również, że wspomniałeś, że musisz tylko zastąpić mocodawcę. W takim przypadku możesz po prostu ponownie użyć formularza FormsIdentity, który został już utworzony dla Ciebie, jak pokazano poniżej.

Context.User = Thread.CurrentPrincipal = new CustomPrincipal(Context.User.Identity /*, add roles here if desired*/); 
+0

Dzięki za odpowiedź i dodatkową wskazówkę. – Pickels