zoukankan      html  css  js  c++  java
  • HttpContext.Current.User is null after installing .NET Framework 4.5

    故障原因:从framework4.0到framework4.5的升级过程中,原有的form认证方式发生了变化,所以不再支持User.Identity.Name原有存储模式(基于cookie),要恢复这个功能,我也是偶然间发现的。
    一下五种解决方案根据具体情况供参考
    1、降级处理,把你的framework降到4.0
    <compilation debug="true " targetFramework=" 4.0" />
        < httpRuntime targetFramework ="4.0" />
     
    2、set the Current User myself in the Global.asax:(不用降级只需加上这段代码)
     private void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
        if (cookie != null)
        {
            FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
            HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(new FormsIdentity(ticket), new string[0]);
        }
    }
     3、 <machineKey validationKey="12..." 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 you'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 do get it working, I'd scrap it all in favor of claims and using the SessionAuthenticationModule which is new and built into 4.5. It is far simpler and will serialize all of your roles into a cookie for you.

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

    5

    Make sure that the machineKey compatibility mode is the same between all applications:
    <machineKey compatibilityMode="Framework20SP1" />

    (The above is the default for 2.0 / 4.0 applications but is not the default for 4.5 applications, so it will have to be set explictly in the 4.5 application.) 

  • 相关阅读:
    IO流图片加密。
    IO流(流的标准处理异常代码)
    字节流写出中文。
    字节流读取中文。
    IO流BufferedInputStream和BufferOutputStream拷贝。
    Collection集合。
    《C#并发编程经典实例》学习笔记—2.3 报告任务
    《C#并发编程经典实例》学习笔记—2.2 返回完成的任务
    《C#并发编程经典实例》学习笔记—2.1 暂停一段时间
    《C#并发编程经典实例》学习笔记—异步编程关键字 Async和Await
  • 原文地址:https://www.cnblogs.com/xiaoweizi/p/3848076.html
Copyright © 2011-2022 走看看