2012-10-21 118 views
0

我正在使用我自己的身份驗證方案,基於非Cookie。我希望能夠手動設置Controller.User.Identity以及我通常在FormsAuthentication Cookie中設置的用戶名和其他數據。目前,用戶身份由FormsAuthentication設置(我不知道它是如何從加密的cookie中讀取的)。 FormsAuthentication如何做到這一點?我會怎麼做?手動設置IIdentity

回答

0
System.Web.HttpContent.Current.User = new GenericPrincipal(
    new GenericIdentity("username", "CUSTOM"), 
    new string[]()); 
System.Threading.Thread.CurrentPrincipal = System.Web.HttpContent.Current.User; 
+0

爲什麼我們將它設置爲2個不同的地方?什麼是'Thread.CurrentPrincipal'和'Current.User'? –

+0

@Lolcoder請參閱[這裏](http://stackoverflow.com/questions/3057937/difference-between-http-context-user-and-thread-currentprincipal-and-when-to-use)。他們有不同的生命週期。而且,依賴關係可以使用一個或另一個(例如,依賴關係A可以使用'System.Thread.CurrentPrincipal',而依賴關係B可以使用'System.Web.HttpContext.Current.User') –