【发布时间】:2015-02-26 13:32:55
【问题描述】:
IIS 抛出:
对象引用未设置为对象的实例。
当它应该将经过身份验证的用户重定向到 /Home/Index
我已经调查了它可能发生的原因,但找不到原因
这条线是:
return RedirectToAction("Index", "Home");
stacktrace:(这是我得到的唯一信息)
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.Mvc.AuthorizeAttribute.AuthorizeCore(HttpContextBase httpContext) +30
System.Web.Mvc.AuthorizeAttribute.OnAuthorization(AuthorizationContext filterContext) +160
System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor) +97
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) +445
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +129
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +287
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__17(AsyncCallback asyncCallback, Object asyncState) +30
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +129
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +338
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +129
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +282
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +15
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__2(AsyncCallback asyncCallback, Object asyncState) +71
System.Web.Mvc.Async.WrappedAsyncResult`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +129
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +236
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
编辑 1: 该应用程序在我的计算机上本地运行。我正在处理所有文件夹并发送到服务器。然后,我尝试在服务器中本地访问,我得到了这个。服务器上的 IIS 对应用程序池具有相同的配置,我相信它的配置是正确的,至少像我一样在本地使用。我试图让它在服务器本地工作,然后我将配置为远程使用。
编辑 2:
这是/Home/Index:
namespace gedaiapp.Controllers
{
[Authorize]
public class HomeController : Controller
{
public ActionResult Index()
{
return PartialView();
}
}
}
和/Index/Login(类有授权属性,只有这个特定的方法有[AllowAnonymous])所以还没有认证的用户可以登录
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, FormCollection form)
{
try
{
//Verifica se logon será feito utilizando certificado digital ou não
string isDigitalCertified = form["hasDigital"];
if (!string.IsNullOrEmpty(isDigitalCertified))
{
string[] isDCArr = isDigitalCertified.Split(',');
if (!string.IsNullOrEmpty(isDCArr[0]))
{
string isDC = isDCArr[0];
//Se login for utilizando certificado digital
if (isDC == "true")
{
//Resgata subject do certificado digital
string certDadosStr = "";
do
{
certDadosStr = Request.ClientCertificate.Subject;
} while (certDadosStr == "");
//Resgata cpf ou cnpj do certificado digital
string[] certDadosArr = certDadosStr.Split(',');
int Count = certDadosArr.Count();
//Razão social é sempre o último elemento no padrão ICP-Brasil
string razaoSocial = certDadosArr[Count - 1];
string[] razaoSocialArr = razaoSocial.Split(':');
Count = razaoSocialArr.Count();
string key = razaoSocialArr[Count - 1];
//Resgata Guid do usuário
MembershipUser user = Membership.GetUser(model.UserName);
Guid userID = (Guid)user.ProviderUserKey;
//Verifica se (cpf ou cnpj) do usuário efetuando o login é o mesmo do cadastrado no sistema
using (gedaiappEntities context = new gedaiappEntities())
{
var keyNumberObj = from a in context.sistema_UsersCertified
where a.userID == userID
select a.keyNumber;
string keyNumber = keyNumberObj.First();
//Se autenticidade for positiva (redireciona)
if (keyNumber == key)
{
FormsAuthentication.SetAuthCookie(model.UserName, false);
return RedirectToAction("Index", "Home");
}
else
{
return RedirectToAction("Login", "Account");
}
}
}//Caso login seja sem certificado digital
else
{
MembershipProvider mp = Membership.Provider;
if (mp.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, false);
try
{
return RedirectToAction("Index", "Home");
}
catch (Exception e)
{
return Content("Erro: " + e);
}
}
return RedirectToAction("Login", "Account");
}
}
else
{
//return Content("Erro Catastrófico: Não foi possivel identificar se login é com certificado digital ou não.");
return RedirectToAction("Login", "Account");
}
}
else
{
//return Content("Erro Catastrófico: Valor do checkbox hasDigital não foi enviado.");
return RedirectToAction("Login", "Account");
}
}
catch (Exception e)
{
return Content("Erro: " + e);
}
}
有什么帮助吗?
【问题讨论】:
-
此堆栈表明在授权过滤器处理期间发生了错误(相对于 RedirectToAction 行)。该方法的其余部分是什么样的?您的方法是否有 auth 过滤器,也许它们有问题?
-
@Peter 我已经编辑了我的帖子。检查编辑 2。感谢您的关注。
-
您的本地开发环境和服务器之间的托管差异是什么?您是在 IIS 或开发 Web 服务器 (Casini) 上托管开发吗?如果是 IIS,什么版本(开发和服务器)?问题还不明显。
-
@Peter dev 是一个 IIS7 Web 服务器,服务器是 IIS6。有问题吗?
-
有可能。在处理 MVC 应用程序时,IIS 6 和 7 之间存在差异。虽然,您呈现的错误似乎与 IIS 版本无关。但是,我想不出一个理由。您确定抛出异常的行吗?你提出的这条线似乎是一个非常不可能的候选人。我会怀疑该方法中的其他内容。也许你可以尝试一个空方法(只返回
Content("test")),使用相同的属性来测试属性行为。