我在主机发布项目后有这个问题,在开发环境中一切正常! 在我发布的MVC 4.0网站中,当经过身份验证的用户尝试上传图片时,该用户已被注销并重定向到登录页面。 我已使用以下代码上传图片并成功在本地工作:
private void TryUploadImages(Product product)
{
const string emptyImage = "empty.jpg";
try
{
for (int idx = 0; idx < 3; idx++)
{
if ((Request.Files.Count < 3) ||
(Request.Files[idx] == null) ||
(Request.Files[idx].ContentLength > 1024 * 1024 * 5) ||
(Request.Files[idx].ContentLength <= 0))
{
if ((idx == 0 && string.IsNullOrEmpty(product.ImageFilename)) ||
(idx == 1 && string.IsNullOrEmpty(product.ThumbnailImage)) ||
(idx == 2 && string.IsNullOrEmpty(product.AttributesImage)))
throw new Exception(GlobalResources.Global_Image_Restrictions_Error);
continue;
}
HttpPostedFileBase uploadedFile = Request.Files[idx];
string fileName = Path.GetFileName(uploadedFile.FileName);
using (var img = Image.FromStream(uploadedFile.InputStream))
{ bool temp = img.Width > 0; }
if (!string.IsNullOrEmpty(fileName))
{
string[] filenames = {"product", "product-thumb", "attribute"};
fileName = string.Format("{0}-{1}{2}",
filenames[idx],
Guid.NewGuid().ToString().Replace("-", string.Empty),
Path.GetExtension(fileName));
var physicalPath = Path.Combine(Server.MapPath("~/Images/sitepx/products/"), fileName);
uploadedFile.SaveAs(physicalPath);
switch (idx)
{
case 0:
product.ImageFilename = fileName;
break;
case 1:
product.ThumbnailImage = fileName;
break;
case 2:
product.AttributesImage = fileName;
break;
}
}
else
{
switch (idx)
{
case 0:
product.ImageFilename = emptyImage;
break;
case 1:
product.ThumbnailImage = emptyImage;
break;
case 2:
product.AttributesImage = emptyImage;
break;
}
}
}
}
catch (Exception ex)
{
ViewBag.UploadError = ex.Message;
product.ImageFilename = emptyImage;
}
}
并在此操作方法中调用它:
[AllowUploadSafeFiles]
[AllowUploadSpecialFilesOnly(".jpg,.jpeg,.gif,.png,.bmp")]
[HttpPost]
public virtual ActionResult Edit(Product product)
{
if (ModelState.IsValid)
{
TryUploadImages(product);
product.ModifiedOn = DateTime.Now;
_db.Entry(product).State = EntityState.Modified;
_db.SaveChanges();
return RedirectToAction(MVC.Product.ActionNames.Index);
}
ViewBag.CategoryId = new SelectList(_db.Categories, "CategoryId", "Name", product.CategoryId);
ViewBag.ProductTypeId = new SelectList(_db.ProductTypes, "ProductTypeId", "Name", product.ProductTypeId);
return View(product);
}
此外,我为特定角色授权控制器,并出于安全原因禁用Web.config中的Sessions:
<httpModules>
<-- blah blah blah ... -->
<!-- Disable Session -->
<remove name="Session" />
</httpModules>
<sessionState mode="Off" />
如果您仍需要其他信息,请随时告诉我们。 感谢
=====已编辑(添加身份验证详细信息)=====
也许我在错误的地方找麻烦,我的登录方法是这样的:
[AllowAnonymous]
public virtual ActionResult Login(string returnUrl)
{
if (User.Identity.IsAuthenticated)
if (!string.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
return RedirectToLocal(returnUrl);
else
return Redirect(ReturnRedirectUrl(returnUrl));
ViewBag.ReturnUrl = returnUrl;
ViewBag.Roles = GetAllAccountRoles();
return View();
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public virtual ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, model.RememberMe))
{
var location = ReturnRedirectUrl(returnUrl);
return string.IsNullOrEmpty(location)
? RedirectToAction(MVC.Account.Login())
: RedirectToLocal(location);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", GlobalResources.Account_Login_ModelError);
return View(model);
}
这是登录函数中使用的Role base ReturnRedirectUrl:
private string ReturnRedirectUrl(string returnUrl)
{
if (string.IsNullOrEmpty(returnUrl) || !Url.IsLocalUrl(returnUrl))
{
foreach (var role in Roles.GetAllRoles().Where(Roles.IsUserInRole))
{
switch (role)
{
case "info":
returnUrl = Url.Action(MVC.SiteManage.Index(1));
break;
case "support":
returnUrl = Url.Action(MVC.SiteManage.Index(2));
break;
case "sales":
returnUrl = Url.Action(MVC.SiteManage.Index(3));
break;
case "admin":
returnUrl = Url.Action(MVC.SiteManage.Index(6));
break;
case "club-member":
returnUrl = Url.Action(MVC.SiteManage.Index());
break;
case "vendor-reseller":
returnUrl = Url.Action(MVC.SiteManage.Index());
break;
case "sales-reseller":
returnUrl = Url.Action(MVC.SiteManage.Index());
break;
}
}
}
return returnUrl;
}