ASP.NET MVC를 사용하여 업로드 (세션 및 인증)
-
19-09-2019 - |
문제
내가 사용하는 경우, uplodify가 사용하는 조치 또는 컨트롤러에서 필터를 승인합니다.http://www.uploadify.com/) 행동에 도달하지 못합니다 ...
또한 세션은 검색되지 않습니다.
검색된 사용자 세션을 찾았습니다.
http://geekswithblogs.net/apopovsky/archive/2009/05/06/working-around-flash-bug-in-sp.net-mvc.aspx
그러나 그것을 사용하는 방법 [Authorize]
필터 및 검색된 세션?
해결책
이를 수정하기 위해 솔루션을 제안합니다 ... 인증 쿠키 값과 세션 ID 쿠키 값을 업로드로 보내고 세션을 검색하기 전에 재현하십시오.
다음은보기에서 이루어질 코드입니다.
<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 }
});
그리고 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);
}
그리고 Voila, 그 방법으로 그것은 완전히 투명합니다.
도움이되기를 바랍니다! ;)
편집 : scriptData 대신 FormData를 사용하십시오
다른 팁
이 솔루션은 훌륭하게 작동합니다. 누구든지 원하는 경우 코드를 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
JavaScript 변수 할당의 부분은 다음과 같습니다.
var auth = "<%=IIf(Request.Cookies(FormsAuthentication.FormsCookieName) Is Nothing, "", Request.Cookies(FormsAuthentication.FormsCookieName).Value)%>";
var ASPSESSID = "<%=Session.SessionID%>";
아마도 VB에서 일하는 사람이 그로부터 혜택을받을 수 있습니다.
VB 변환 코드 *의 경우 *<%= 대신 <%#로 코드 블록을 시작하십시오.
즉
var auth='<%# IIf(Request.Cookies(FormsAuthentication.FormsCookieName) Is Nothing, "",
Request.Cookies(FormsAuthentication.FormsCookieName).Value)%>';
var ASPSESSID = '<%# Session.SessionID%>';
제휴하지 않습니다 StackOverflow