【问题标题】:Uploadify (flash file upload) & Integrated Windows AuthenticationUploadify(闪存文件上传)和集成 Windows 身份验证
【发布时间】:2011-02-02 17:49:29
【问题描述】:

我遇到了 Uploadify 的问题,希望有人能提供帮助。我已将 Uploadify 放入我的应用程序中,并且在 dev 中一切正常(使用 VS Web 服务器)。在我将应用程序部署到使用集成 Windows 身份验证的测试环境中之前,一切正常并进行了检查。

当我真正去上传文件时,浏览器会弹出一个登录提示。此时,即使您输入了正确的用户名和密码,请求似乎也没有完成,即使您告诉浏览器记住密码,它仍然会弹出登录提示。

当这种情况开始发生时,我决定启动 Fiddler 看看发生了什么。但是猜猜看,当 Fiddler 运行时,问题不会发生。

不幸的是,我无法让运行 Fiddler 成为运行应用程序的必要条件。因此,有没有人有任何想法。我知道在使用表单身份验证时 Uploadify/flash 存在一些问题,但我不认为它们会扩展到集成 Windows 身份验证。

【问题讨论】:

    标签: flash iis browser file-upload uploadify


    【解决方案1】:

    我看到这个页面,我几乎放弃了,但后来我在 PluralSight 的 Craig 看到了这个 article。这让我想到了从 ASP.Net 而不是 IIS 返回 401,这就是在 IIS 中启用匿名身份验证的原因。

    以下是解决此问题的步骤。

    第 1 步:在 IIS 中启用匿名身份验证和 Windows 身份验证。

    第 2 步:将此代码添加到您的 Global.asax.cs
    致谢/感谢: Uploadify (Session and authentication) with ASP.NET MVC
    注意: 仅在我的版本中POST 请求使用特殊逻辑,因为我只希望此代码适用于 uploadify。换句话说,我删除了 GET 请求的代码。如果您想支持 GET,请查看上面的链接。

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        /* we guess at this point session is not already retrieved by application so we recreate cookie with the session id... */
        try
        {
            string session_param_name = "ASPSESSID";
            string session_cookie_name = "ASP.NET_SessionId";
    
            if (HttpContext.Current.Request.Form[session_param_name] != null)
            {
                UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);
            }
    
        }
        catch
        {
        }
    
        try
        {
            string auth_param_name = "AUTHID";
            string auth_cookie_name = FormsAuthentication.FormsCookieName;
    
            if (HttpContext.Current.Request.Form[auth_param_name] != null)
            {
                UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]);
                return; // this is an uploadify request....get out of here.
            }
    
        }
        catch
        {
        }
    
        // handle the windows authentication while keeping anonymous turned on in IIS.
        // see: https://stackoverflow.com/questions/2549914/uploadify-flash-file-upload-integrated-windows-authentication
    
        if (Request.ServerVariables["LOGON_USER"].Length == 0) // They haven't provided credentials yet
        {
            Response.StatusCode = 401;
            Response.StatusDescription = "Unauthorized";
            Response.End();
            return;
        }
    
        FormsAuthentication.SetAuthCookie(Request.ServerVariables["LOGON_USER"], true); 
    
    }
    
    private void UpdateCookie(string cookie_name, string cookie_value)
    {
        HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
        if (null == cookie)
        {
            cookie = new HttpCookie(cookie_name);
        }
        cookie.Value = cookie_value;
        HttpContext.Current.Request.Cookies.Set(cookie);
    } 
    

    第 3 步:更新调用 uploadify 的 javascript 以包含表单的身份验证密钥和会话密钥。

    <script> 
        var auth = "<% = Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>"; 
        var ASPSESSID = "<%= Session.SessionID %>"; 
    
        $("#uploadifyLogo").uploadify({ 
            ... 
            scriptData: { ASPSESSID: ASPSESSID, AUTHID: auth } 
        }); 
    

    第 4 步:更新您的 web.config

      <system.web>
        ...
        <authentication mode="Forms">
          <forms defaultUrl="/" />
        </authentication>
        ...
    

    【讨论】:

    • 我遇到了与 OP 完全相同的问题,我尝试了您的解决方案,但无法正常工作。由于某种原因 Request.ServerVariables["LOGON_USER"] 始终为空。有什么建议吗?
    • IIS 中是否同时启用了匿名身份验证和 windows 身份验证?
    • 我知道评论/提问已经晚了,但这意味着使用 uploadify 和 Windows 身份验证的解决方案不需要使用 Windows 身份验证?
    • @bzarah 在我看来就是这样。很臭,但我看不到它。使用一些 AJAX 来尽可能地模仿它。
    猜你喜欢
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 2011-10-08
    • 2019-01-31
    • 2018-09-28
    • 1970-01-01
    相关资源
    最近更新 更多