I have encountered the following code that attempts to handle an authentication exception in a custom STS web site. This looks passable, but on calling Controller.Execute
, I get a NullReferenceException
. I would like to avoid the annoying black box of calling this method and rather redirect directly to the action, or throw a custom exception and set up built in error handling to do the redirect when the custom exception is thrown. I actually need some advice on how to do either properly.
var logId = LogProvider.Current.LogError(LogLevel.Fatal,e.InnerException ?? e, "Unexpected error during user authentication: [{0}]", incomingPrincipal.Identity.Name);
var context = HttpContext.Current;
var routeData = new RouteData();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Index");
routeData.Values.Add("errorId", logId);
routeData.Values.Add("exceptionMessage", "");
IController controller = new ErrorController();
var ctx = new RequestContext(new HttpContextWrapper(context), routeData);
controller.Execute(ctx);
context.Response.End();\