使用 ASP.NET MVC 上传(会话和身份验证)
-
19-09-2019 - |
题
当我对 uplodify 使用的操作或控制器使用授权过滤器时(http://www.uploadify.com/) 动作未达到...
而且会话不会被检索。
我发现这个可以检索用户会话:
但如何使用它 [Authorize]
过滤和检索会话?
解决方案
要纠正这一点,我建议你解决......与uploadify发送身份验证cookie值和会话cookie的值,然后重新创建检索会议之前。
这里是在视图中implent的代码:
<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);
}
和瞧,与该方法是完全透明的。
希望帮助一些! ;)
<强> EDITED 强>:使用代替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