Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:
Hello everyone,

I have facing issues in MVC 5 at the run time.


Error is coming below line.

C#
var rc = new RequestContext( new HttpContextWrapper(Context), routeData);
controller.Execute(rc);


Code Details are

C#
[HttpPost]
       [AllowAnonymous]
       [ValidateAntiForgeryToken]
       public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
       {
           if (ModelState.IsValid)
           {
               var user = await UserManager.FindAsync(model.UserName, model.Password);
               if (user != null)
               {
                   await SignInAsync(user, model.RememberMe);
                   // Encrypt the ticket and Create the cookie.
                   EncryptTicketCookies(model);
                   return RedirectToLocal(returnUrl);
               }
               else
               {
                   ModelState.AddModelError("", Messages.InvalidUidPwd);
               }
           }

           // If we got this far, something failed, redisplay form
           return View(model);
       }


C#
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    // Get the authentication cookie
    string cookieName = FormsAuthentication.FormsCookieName;
    HttpCookie authCookie = Context.Request.Cookies[cookieName];

    // If the cookie can't be found, don't issue the ticket, redirect to the login page.
    if (authCookie == null)
    {
         var routeData = new RouteData();
         routeData.Values["controller"] = "Account";
         routeData.Values["action"] = "Login";
         IController controller = null;
         controller = new AccountController();

        var rc = new RequestContext( new HttpContextWrapper(Context), routeData);
        controller.Execute(rc);
    }
}



System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code.
Additional information: The asynchronous action method 'Login' returns a Task, which cannot be executed synchronously.


Please help me?


Thanks in advance!!!
Posted
Updated 3-Aug-14 21:08pm
v6
Comments
Sergey Alexandrovich Kryukov 4-Aug-14 2:30am    
And what is that line of code where this exception was thrown?
—SA
anil.singh581 4-Aug-14 2:49am    
var rc = new RequestContext( new HttpContextWrapper(Context), routeData);
controller.Execute(rc);
anil.singh581 4-Aug-14 3:15am    
I have updated my question please see the above details again.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900