Asp.net mvc 4 MVC4应用程序在登录后不会进入默认页面

Asp.net mvc 4 MVC4应用程序在登录后不会进入默认页面,asp.net-mvc-4,deployment,Asp.net Mvc 4,Deployment,我创建了一个以.NET4.0为目标的MVC4应用程序。部署到我的生产服务器后,它将显示登录页面,但不会重定向到默认页面。但是,当我添加调试时,我可以看到身份验证过程是有效的,但是我得到的错误是一个错误,它表示找不到我的错误页面的视图,然后显示我的错误页面。它似乎不会进入我的“主页/索引”页面——即使我删除了authorize属性。当然,该应用程序在开发中起作用。此外,它不会进入我的注册页面或忘记登录页面 我的登录控制器如下所示: [HttpPost] [AllowAnonymous] [Vali

我创建了一个以.NET4.0为目标的MVC4应用程序。部署到我的生产服务器后,它将显示登录页面,但不会重定向到默认页面。但是,当我添加调试时,我可以看到身份验证过程是有效的,但是我得到的错误是一个错误,它表示找不到我的错误页面的视图,然后显示我的错误页面。它似乎不会进入我的“主页/索引”页面——即使我删除了authorize属性。当然,该应用程序在开发中起作用。此外,它不会进入我的注册页面或忘记登录页面

我的登录控制器如下所示:

[HttpPost]
[AllowAnonymous]
[ValidateAntiforgeryToken]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
    if(ModelStat.IsValid && _userService.Login(model.UserId, model.Password))
    {
        var user = _userService.GetUser(model.UserId);
        var loggedInUser = new LoggedInUser
        {
            // Build the user for custom IPrincipal
        };
        var userData = JsonConvert.SerializeObject(loggedInUser);
        var compressData = StringCompression.Compress(userData);
        var authTicket = new FormsAuthenticationTicket(
            1,
            user.UserId,
            DateTime.Now,
            DateTime.Now.AddHours(1),
            false,
            compressData);
        var encTicket = FormsAuthentication.Encrypt(authTicket);
        if(encTicket != null)
        {
            var faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)
        {
            HttpOnly = true
        };
        Response.Cookies.Add(faCookie);
    }

    user.LastActivityDate = DateTime.Now;
    user.LastLoginDate = DateTime.Now;
    _userService.UpdateUser(user);
    _uow.Commit();

    return Url.IsLocalUrl(returnUrl) ? (ActionResult)Redirect(returnUrl) : RedirectToAction("Index", "Home");
    }
    return View(model);
在my Global.asax中:

protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
    var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];

    if(authCookie != null)
    {
        var decompressedData = StringCompression.Decompress(authTicket.UserData);
        var loggedInUser = JsonConvert.DesrializeObject<LoggedInUser>(decompressedData);
        var currrentUser = new CustomPrincipal(authTicket.Name)
        {
            // Build the CustomPrincipal from the loggedInUser
        };
        if(HttpContext.Current.User.Identity.IsAuthenticated)
        {
            HttpContext.Current.User = currentUser;
        }
    }
}
受保护的无效应用程序\u PostAuthenticateRequest(对象发送方,事件参数e)
{
var authCookie=Request.Cookies[FormsAuthentication.FormScookeName];
if(authCookie!=null)
{
var decompressedData=StringCompression.decompresse(authTicket.UserData);
var loggedInUser=JsonConvert.DesrializeObject(解压缩数据);
var currentuser=new CustomPrincipal(authTicket.Name)
{
//从loggedInUser构建CustomPrincipal
};
if(HttpContext.Current.User.Identity.IsAuthenticated)
{
HttpContext.Current.User=currentUser;
}
}
}
我希望这足以让别人知道我可能做错了什么。不知何故,我觉得我缺少的是一件小事。提前谢谢。 ~InDireStraits

更新:

public class BaseController: Controller
{
    private string _actionKey;
    private const string PermisisionList = "permissionList";
    private Dictionary<string, string> _requiredActionPermissions;
    private static readonly IControllerActionService<ControllerAction> _actionService;

    protected new CustomPrincipal User
    {
        get
        {
            return HttpContext.User as CustomPrincipal;
        }
    }

    public BaseController(IControllerActionService<ControllerAction> actionService)
    {
        _actionService = actionService;
    }


    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // Check to see if the PermissionList is loaded and load if necessary
        if(!CacheLayer.Exists(PermissionList))
        {
            _requiredActionPermissions = _actionService.GetControllerActionDictionary();
            CacheLayer.Add(_requiredActionPermissions, PermissionList);
        }
        else
        {
            _requiredActionPermission = CacheLayer.Get<Dictionary<string, string>>(PermissionList);
        }
        // Get the Controller/Action of the current request
        _actionKey = string.Format("{0}-{1}", filterContext.ActionDescriptor.ControllerDescriptor.ControllerName, filterContext.ActionDescriptor.ActionName);

        // If the user is authenticated, grab the permissions
        if(filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            var userPermissions = User.Permissions;
           if(!_requiredActionPermissions.Values.Any(a=>a.Equals(_actionKey, StringComparison.OrdinalIgnoreCase)))
            {
                return;
            }
        if(userPermissions.Contains(_requiredActionsPermissions.FirstOrDefault(x=>x.Value == _actionKey).Key))
            {
                return;
            }
            filterContext.Result = new RedirectResult("~/Error/ErrorUnauthorized");
            return;
        }
        if(!filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            if(!_requiredActionPermissions.Values.Any(a=>a.Equals(_actionKey, StringComparison.OrdinalIgnoreCase)))
            {
                return;
            }
        }
        if(filterContext.HttpContext.Request.Url == null)
        { 
            return;
        }
        if(filterContext.HttpContext.Request.Url.AbsolutePath == FormsAuthentication.LoginUrl)
        {
            return;
        }
        var redirectUrl = string.Format("?returnUrl={0}", filterContext.HttpContext.Request.Url.PathAndQuery);
         filterContext.HttpContext.Response.Redirect(FormsAuthentication.LoginUrl + redirectUrl, true);
    }
在进行了更多的故障排除之后,问题似乎与我使用BaseController指定权限这一事实有关,但我仍然不明白为什么应用程序在我的开发环境中按预期工作,而不是在生产环境中工作。为了验证我的IIS设置,我在没有.NET 4.5的生产环境中安装了默认的MVC4应用程序,并运行了该应用程序。我用的是VS2012,所以我有4.5。即使是针对.NET4.0,我是否可以以某种方式引入.NET4.5类或功能?无论如何,这是我的BaseController代码:

public class BaseController: Controller
{
    private string _actionKey;
    private const string PermisisionList = "permissionList";
    private Dictionary<string, string> _requiredActionPermissions;
    private static readonly IControllerActionService<ControllerAction> _actionService;

    protected new CustomPrincipal User
    {
        get
        {
            return HttpContext.User as CustomPrincipal;
        }
    }

    public BaseController(IControllerActionService<ControllerAction> actionService)
    {
        _actionService = actionService;
    }


    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // Check to see if the PermissionList is loaded and load if necessary
        if(!CacheLayer.Exists(PermissionList))
        {
            _requiredActionPermissions = _actionService.GetControllerActionDictionary();
            CacheLayer.Add(_requiredActionPermissions, PermissionList);
        }
        else
        {
            _requiredActionPermission = CacheLayer.Get<Dictionary<string, string>>(PermissionList);
        }
        // Get the Controller/Action of the current request
        _actionKey = string.Format("{0}-{1}", filterContext.ActionDescriptor.ControllerDescriptor.ControllerName, filterContext.ActionDescriptor.ActionName);

        // If the user is authenticated, grab the permissions
        if(filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            var userPermissions = User.Permissions;
           if(!_requiredActionPermissions.Values.Any(a=>a.Equals(_actionKey, StringComparison.OrdinalIgnoreCase)))
            {
                return;
            }
        if(userPermissions.Contains(_requiredActionsPermissions.FirstOrDefault(x=>x.Value == _actionKey).Key))
            {
                return;
            }
            filterContext.Result = new RedirectResult("~/Error/ErrorUnauthorized");
            return;
        }
        if(!filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            if(!_requiredActionPermissions.Values.Any(a=>a.Equals(_actionKey, StringComparison.OrdinalIgnoreCase)))
            {
                return;
            }
        }
        if(filterContext.HttpContext.Request.Url == null)
        { 
            return;
        }
        if(filterContext.HttpContext.Request.Url.AbsolutePath == FormsAuthentication.LoginUrl)
        {
            return;
        }
        var redirectUrl = string.Format("?returnUrl={0}", filterContext.HttpContext.Request.Url.PathAndQuery);
         filterContext.HttpContext.Response.Redirect(FormsAuthentication.LoginUrl + redirectUrl, true);
    }
公共类BaseController:控制器
{
私有字符串_actionKey;
private const string permissionList=“permissionList”;
专用词典_需要操作权限;
私有静态只读IControllerationService\u actionService;
受保护的新CustomPrincipal用户
{
得到
{
返回HttpContext.User作为CustomPrincipal;
}
}
公共BaseController(IControllerationService操作服务)
{
_actionService=actionService;
}
受保护的覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
{
//检查许可列表是否已加载,必要时加载
如果(!CacheLayer.Exists(PermissionList))
{
_requiredActionPermissions=\u actionService.GetControllerActionDictionary();
添加(_requiredActionPermissions,PermissionList);
}
其他的
{
_requiredActionPermission=CacheLayer.Get(PermissionList);
}
//获取当前请求的控制器/操作
_actionKey=string.Format(“{0}-{1}”,filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,filterContext.ActionDescriptor.ActionName);
//如果用户已通过身份验证,请获取权限
if(filterContext.HttpContext.User.Identity.IsAuthenticated)
{
var userPermissions=User.Permissions;
if(!_requiredActionPermissions.Values.Any(a=>a.Equals(_actionKey,StringComparison.OrdinalIgnoreCase)))
{
返回;
}
if(userPermissions.Contains(_requiredActionsPermissions.FirstOrDefault(x=>x.Value==_actionKey.Key))
{
返回;
}
filterContext.Result=新的重定向结果(“~/Error/ErrorUnauthorized”);
返回;
}
如果(!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
if(!_requiredActionPermissions.Values.Any(a=>a.Equals(_actionKey,StringComparison.OrdinalIgnoreCase)))
{
返回;
}
}
if(filterContext.HttpContext.Request.Url==null)
{ 
返回;
}
if(filterContext.HttpContext.Request.Url.AbsolutePath==FormsAuthentication.LoginUrl)
{
返回;
}
var redirectUrl=string.Format(“?returnUrl={0}”,filterContext.HttpContext.Request.Url.PathAndQuery);
filterContext.HttpContext.Response.Redirect(FormsAuthentication.LoginUrl+redirectUrl,true);
}
更新2:在登台服务器上安装了.NET 4.52,应用程序现在可以按预期工作。问题是我无法在生产服务器上安装。我不明白4.5修复了什么,而4.0似乎不方便。Helllllppp!!!!!

我补充道,答案可以总结一下我的路线配置中的一个额外参数在4.5中起作用,但在4.0中不起作用。将跟进链接问题。谢谢