2
Answers

query about same login page for both users

umair mohsin

umair mohsin

Apr 04
305
1

this is my code.

public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    email = model.Email;
    if (!ModelState.IsValid)
    {
        return View(model);  
    }
    var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
    switch (result)
    {
        case SignInStatus.Success:
            return RedirectToLocal(returnUrl);
        case SignInStatus.LockedOut:
            return View("Lockout");
        case SignInStatus.RequiresVerification:
            return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
        case SignInStatus.Failure:
        default:
            return View(model);
    }
}

as above code shows i am using identity api in mvc,what i want to do i have created a single login page for both the users and i am checking user roles and redirecting them to the appropriate view like this:

private ActionResult RedirectToLocal(string returnUrl)
{
 
    var data=new  object();

    if (User.Identity.IsAuthenticated)
    {
        try
        {
            if (User.IsInRole("emp"))
            {
                data = empcontext.GetModel().
                    Where(x => x.Email == email).Select(x => x.Email).
                    FirstOrDefault();
                return RedirectToAction("Index", "Employer", data);

            }
            else if (User.IsInRole("can"))
            {
                data = context.GetModel().
                      Where(x => x.Email == email).
                      Select(x => x.Email).
                      FirstOrDefault();
                return RedirectToAction("Index", "Js", data);
            }

            else (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
        }
        catch (Exception ex) { }
    }
}

above code gives squiggly red line the error says 

'AccountController.RedirectToLocal(string)': not all code paths return a value

redirectto localis necessary to redirect the user to the same page if the user is not logged in

Answers (2)