1
我在我創建了一個網站使用Flash上傳。我需要將大文件上傳到服務器。問題是這個上傳器使用閃存。當提交數據的cookie不會發送回服務器,所以爲此我無法驗證用戶,這將失敗。有沒有辦法強制將cookie發送回服務器?如果這是不可能的,是否有其他的方式將數據與回送餅乾的其他組件上傳。asp.net MVC 3 Flash上傳忽略餅乾(二選一?)
我在我創建了一個網站使用Flash上傳。我需要將大文件上傳到服務器。問題是這個上傳器使用閃存。當提交數據的cookie不會發送回服務器,所以爲此我無法驗證用戶,這將失敗。有沒有辦法強制將cookie發送回服務器?如果這是不可能的,是否有其他的方式將數據與回送餅乾的其他組件上傳。asp.net MVC 3 Flash上傳忽略餅乾(二選一?)
有幾個網站,這個問題進行了討論。解決方案是,手動將授權信息通過flash中的另一個post變量傳回給MVC。我找到的實現是TokenizedAuthorizeAttribute
。
/// <summary>
/// A custom version of the <see cref="AuthorizeAttribute"/> that supports working
/// around a cookie/session bug in Flash.
/// </summary>
/// <remarks>
/// Details of the bug and workaround can be found on this blog:
/// http://geekswithblogs.net/apopovsky/archive/2009/05/06/working-around-flash-cookie-bug-in-asp.net-mvc.aspx
/// </remarks>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class TokenizedAuthorizeAttribute : AuthorizeAttribute
{
/// <summary>
/// The key to the authentication token that should be submitted somewhere in the request.
/// </summary>
private const string TOKEN_KEY = "AuthenticationToken";
/// <summary>
/// This changes the behavior of AuthorizeCore so that it will only authorize
/// users if a valid token is submitted with the request.
/// </summary>
/// <param name="httpContext"></param>
/// <returns></returns>
protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
{
string token = httpContext.Request.Params[TOKEN_KEY];
if (token != null)
{
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token);
if (ticket != null)
{
FormsIdentity identity = new FormsIdentity(ticket);
string[] roles = System.Web.Security.Roles.GetRolesForUser(identity.Name);
GenericPrincipal principal = new GenericPrincipal(identity, roles);
httpContext.User = principal;
}
}
return base.AuthorizeCore(httpContext);
}
}
繼評論中的Link將幫助您進一步。
會工作,如果沒有一個外部安全meganism檢查每個結果。 – Patrick
不明白你的意思。 – DanielB
我使用的檢查everyrequest特定cookie中的身份服務器。如果該cookie不存在,它將終止連接。 – Patrick