使用AJAX时ASP.NET MVC用户身份验证失败
我正在编写一个ASP.NET MVC3 web应用程序,当我使用Ajax调用操作方法时,用户身份验证失败(用户未经身份验证)。我的电话是这样的:使用AJAX时ASP.NET MVC用户身份验证失败,asp.net,asp.net-mvc-3,authentication,jquery,Asp.net,Asp.net Mvc 3,Authentication,Jquery,我正在编写一个ASP.NET MVC3 web应用程序,当我使用Ajax调用操作方法时,用户身份验证失败(用户未经身份验证)。我的电话是这样的: $(function () { $("#picture").makeAsyncUploader({ upload_url: '@Url.Action("AsyncUpload", "Profile")', flash_url: '/Scrip
$(function () {
$("#picture").makeAsyncUploader({
upload_url: '@Url.Action("AsyncUpload", "Profile")',
flash_url: '/Scripts/swfupload.swf',
button_image_url: '/Scripts/blankButton.png'
});
});
其中makeAsyncUploader是一个单独js文件中的函数,用于处理所有AJAX内容。我已尝试调试该应用程序,但似乎没有随请求一起向我发送cookie。有人知道问题出在哪里吗?我知道这是一个很老的问题,但我今天遇到了完全相同的问题,所以我会回答它 Firefox的Flash plg中有一个bug。上传文件时不发送cookie。 我的解决方案: 1) 创建新的授权属性
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class FlashAuthorizeAttribute : AuthorizeAttribute
{
private const string AUTH_TOKEN = "AuthenticationToken4Flash";
protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
{
string authToken = httpContext.Request.Params[AUTH_TOKEN];
if (authToken != null)
{
FormsAuthenticationTicket authForm = FormsAuthentication.Decrypt(authToken);
if (authForm != null)
{
FormsIdentity formIdentity = new FormsIdentity(authForm);
string[] userRoles = System.Web.Security.Roles.GetRolesForUser(formIdentity.Name);
GenericPrincipal userPrincipal = new GenericPrincipal(formIdentity, userRoles);
httpContext.User = userPrincipal;
}
}
return base.AuthorizeCore(httpContext);
}
}
2) 控制器
[FlashAuthorize]
public ActionResult AsyncUpload()
{
HttpPostedFileBase file = Request.Files[0];
}
3) 修改js(formData、scriptData对我不起作用,所以我添加了一个查询字符串)
我希望它能帮助某人显示您的makeAsyncUploader函数和AsyncUpload conotroller操作您能从控制器中包含一些代码吗?正是我想要的:)
upload_url: '@Url.Action("AsyncUpload", "Profile")' +'?AuthenticationToken4Flash=' + '@(Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value)',