HttpContext.Current.User is null after installing. NET Framework 4.5

Source: Internet
Author: User

Failure reason: From framework4.0 to framework4.5 upgrade process, the original form authentication mode has changed, so no longer support User.Identity.Name original storage mode (based on cookies), to restore this function, I also discovered by accident. Five solutions for reference 1, downgrade processing, reduce your framework to 4.0<compilationDebug="true"targetframework="4.0"/><HttpRuntimetargetframework="4.0"/> 2. Set the current User myself in the Global.asax: (just add this code without downgrading)
 Private void Application_AuthenticateRequest(ObjectSender, EventArgse){ HttpCookieCookies= HttpContext. Current.Request.Cookies[FormsAuthentication.FormsCookieName]; if (Cookies!= NULL) { FormsAuthenticationTicketTicket= FormsAuthentication.Decrypt(Cookies.Value); HttpContext. Current.User = New System.Security.Principal.GenericPrincipal(New formsidentity ( ),   Span class= "KWD" >new  string  [ 0  ]);   < Span class= "pun" >
3, <machinekey validationkey= "..." decryptionkey= "34 ..."
validation= "SHA1" decryption= "AES"
Compatibilitymode= "Framework20sp1"/>4, Sessionauthenticationmodule

Since you ' re in 4.5 and Since you ' re manually issuing the cookie and Since you ' re already intercepting the cookie, then yo U ' re not really using the forms Auth HTTP module at all. I ' d remove that for now. Also, change your module to handle authenticaterequest. See how that works.

Then once you does get it working, I ' d scrap it all in favor of claims and using the sessionauthenticationmodule which are NE W and built into 4.5. It is far simpler and would serialize all of the your roles into a cookies for you.

Http://msdn.microsoft.com/zh-cn/library/system.identitymodel.services.sessionauthenticationmodule (v=vs.110). aspx

5

Make sure, the MachineKey compatibility mode is the same between all applications:
<machinekey compatibilitymode= "Framework20sp1"/>

(The above is the default for 2.0/4.0 applications and is not the default for 4.5 applications and so it would have To BES set explictly in the 4.5 application.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.