使用ASP.NET MVC上传(会话和身份验证)

Dra*_*ouf 34 authentication asp.net-mvc session uploadify

当我对uplodify(http://www.uploadify.com/)使用的操作或控制器使用"授权"过滤器时,操作无法到达...

此外,不会检索会话.

我发现这是检索用户会话:

http://geekswithblogs.net/apopovsky/archive/2009/05/06/working-around-flash-cookie-bug-in-asp.net-mvc.aspx

但是如何将它与[Authorize]过滤器和检索会话一起使用?

Dra*_*ouf 62

为了纠正这个问题,我建议你一个解决方案...使用uploadify发送auth cookie值和会话ID cookie值,并在检索会话之前重新创建它.

这是在视图中实现的代码:

<script>
    var auth = "<% = Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value %>";
    var ASPSESSID = "<%= Session.SessionID %>";

    $("#uploadifyLogo").uploadify({
        ...
        formData: { ASPSESSID: ASPSESSID, AUTHID: auth }
    });
Run Code Online (Sandbox Code Playgroud)

然后在Global.asax中:

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]);
            }
            else if (HttpContext.Current.Request.QueryString[session_param_name] != null)
            {
                UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[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]);
            }
            else if (HttpContext.Current.Request.QueryString[auth_param_name] != null)
            {
                UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]);
            }

        }
        catch
        {
        }
    }

    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);
    }
Run Code Online (Sandbox Code Playgroud)

瞧,用这种方法,它是完全透明的.

希望它有所帮助!! ;)

编辑:使用formData而不是scriptData

  • 对于较新版本的Uploadify,formData而不是scriptData (6认同)
  • 确实,这非常有用:我感谢你.我在想这个问题必须涉及Flash检索会话cookie的方式(如果有的话),但我只是没有看到发生了什么,直到我看到这个我从未意识到你实际上可以更新传入的cookie这种方式.你是一个gorram英雄,谢谢.=) (2认同)
  • 将sessionId放在脚本中是个坏主意,因为它使得它很容易受到会话劫持的攻击(除非您通过SSL提供脚本). (2认同)
  • @Adam Nofsinger - 这是事实,但不需要更多地打开自己.使用cookie,您可以将它们设置为HTTPOnly(不是我知道的完整证据),但是使用脚本,您可以将自己打开到恶意js.这是一个类似的响应 - 不需要会话ID在您的js中.http://stackoverflow.com/questions/4538174/uploadify-ashx-file-context-session-gets-null-in-mozilla/4538433#4538433对不起,我不是故意挑剔,你的答案很好(帮助我出去了,但最近我做了一些安全研究,引起了我的注意. (2认同)
  • +1000真的帮助我.-999有*两个*空`catch`子句.伤了我的眼睛...... (2认同)

Lan*_*och 5

此解决方案效果很好.如果有人想要,我将代码翻译成vb:

    Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As System.EventArgs)
    'we guess at this point session is not already retrieved by application so we recreate cookie with the session id...
    Try
        Dim session_param_name = "ASPSESSID"
        Dim session_cookie_name = "ASP.NET_SessionId"

        If Not HttpContext.Current.Request.Form(session_param_name) Is Nothing Then
            UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form(session_param_name))
        ElseIf Not HttpContext.Current.Request.QueryString(session_param_name) Is Nothing Then
            UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString(session_param_name))
        End If
    Catch ex As Exception
    End Try


    Try
        Dim auth_param_name = "AUTHID"
        Dim auth_cookie_name = FormsAuthentication.FormsCookieName

        If Not HttpContext.Current.Request.Form(auth_param_name) Is Nothing Then
            UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form(auth_param_name))
        ElseIf Not HttpContext.Current.Request.QueryString(auth_param_name) Is Nothing Then
            UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString(auth_param_name))
        End If
    catch ex As Exception
    End Try
End Sub

Private Sub UpdateCookie(ByVal cookie_name As String, ByVal cookie_value As String)
    Dim cookie = HttpContext.Current.Request.Cookies.Get(cookie_name)
    If cookie Is Nothing Then
        cookie = New HttpCookie(cookie_name)
    End If
    cookie.Value = cookie_value
    HttpContext.Current.Request.Cookies.Set(cookie)
End Sub
Run Code Online (Sandbox Code Playgroud)

这是javascript变量赋值的部分:

var auth = "<%=IIf(Request.Cookies(FormsAuthentication.FormsCookieName) Is Nothing, "", Request.Cookies(FormsAuthentication.FormsCookieName).Value)%>";
var ASPSESSID = "<%=Session.SessionID%>";
Run Code Online (Sandbox Code Playgroud)

也许在VB工作的人可以从中受益.