Just Login and Log off base on Identity 2 or any Claims-based identity RRS feed

  • Question

  • User1184281746 posted

    Hi dear ,

    I have an WebApplication base on ASP.Net MVC 5 .My database was created in sqlserver( i use database first ) so for authentication i dont need to create new tables because i have my tables.

    I want just Set Cookie base on Identity 2 and Log off

    var result = objUser.Login(username ,password);//check exist
                if(result.IsValid == true)
    // set cookie and login

    what is best solution .How can i do it?

    please help me


    Saturday, November 28, 2015 5:26 AM


  • User614698185 posted

    Hi nazlin,

    Firstly, install NuGet packages about Microsoft.Owin.Security.Cookies. This is the package that actually enables cookie based authentication.

    Then, add the POST action (POST /auth/login) that validates the provided credentials and logs the user in:

    public ActionResult LogIn(LogInModel model)
        if (!ModelState.IsValid)
            return View();
        // Don't do this in production!
        if (model.Email == "admin@admin.com" && model.Password == "password")
            var identity = new ClaimsIdentity(new[] {
                    new Claim(ClaimTypes.Name, "Ben"),
                    new Claim(ClaimTypes.Email, "a@b.com"),
                    new Claim(ClaimTypes.Country, "England")
            var ctx = Request.GetOwinContext();
            var authManager = ctx.Authentication;
            return Redirect(GetRedirectUrl(model.ReturnUrl));
        // user authN failed
        ModelState.AddModelError("", "Invalid email or password");
        return View();
    private string GetRedirectUrl(string returnUrl)
        if (string.IsNullOrEmpty(returnUrl) || !Url.IsLocalUrl(returnUrl))
            return Url.Action("index", "home");
        return returnUrl;

    Logging Out: Add the following action to AuthController:

    public ActionResult LogOut()
        var ctx = Request.GetOwinContext();
        var authManager = ctx.Authentication;
        return RedirectToAction("index", "home");

    For more information, please refer to the following documents:



    Best Regards,

    Candice Zhou

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 30, 2015 7:51 AM