Tugsuu Ganbat
Tugsuu Ganbat

Reputation: 61

Asp.net MVC-5 Identity suddenly not working

I'm developing web in Asp.Net Mvc5. But today suddenly I can't login /Using identity/.

Here's my login action:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginViewModel input, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            string personalNo = AppStaticConfig.ServiceController.PupilLogin(input.Code, input.Password, ModelState);
            if (!string.IsNullOrEmpty(personalNo))
            {
                PupilProfile pp = AppStaticConfig.ServiceController.PupilDetail(personalNo, AppStaticConfig.SchoolID, ModelState);

                var identity = new ClaimsIdentity(new[] {
                    new Claim(ClaimTypes.Name, pp.pupil.LastName.Substring(0, 1) + "." + pp.pupil.FirstName),
                    new Claim(ClaimTypes.NameIdentifier, personalNo),
                    new Claim("pupilCode", input.Code)
                },
                    DefaultAuthenticationTypes.ApplicationCookie,
                    ClaimTypes.Name, ClaimTypes.Role); 

                    identity.AddClaim(new Claim(ClaimTypes.Role, "guest"));

                    Authentication.SignIn(new AuthenticationProperties{ IsPersistent = true }, identity);

                TempData["pp"] = pp;
                return RedirectToAction("PupilProfile", "Pupil");
            }
        }

        return View(input);
    }

And here's Pupil controller's PupilProfile action which redirected after successful login:

[Authorize]
public class PupilController : Controller
{
    [HttpGet]
    public ActionResult PupilProfile()
    {
        PupilProfile pp = TempData["pp"] != null ? TempData["pp"] as PupilProfile : AppStaticConfig.ServiceController.PupilDetail(User.Identity.GetUserId(), AppStaticConfig.SchoolID, ModelState);
        return View(pp);
    }
}

I think the problem is User.Identity not setting in Authentication.SignIn.

Any1 has idea what's wrong?

Upvotes: 0

Views: 1584

Answers (2)

Victor C
Victor C

Reputation: 70

i just switched to different browser and that was all.

Upvotes: 0

Tugsuu Ganbat
Tugsuu Ganbat

Reputation: 61

Problem solved. Was some environment problem I think.

What I was tried to fix:

  1. Clear all cache cookie in browser. /But was no success/
  2. Reset VS2013 settings from 'Import export menu'
  3. Restart OS /my os:Win8.1/
  4. Relaunched and problem was solved.

Hope it help some1.

Upvotes: 2

Related Questions