3

I'm switching from mvc 3 to mvc 5, I know they changed the authentication in .Net 4.5.1 (OWIN authentication).

all I need it to log in the website so the Request.IsAuthenticated is true, I don't want the membership of Microsoft, I want to use my database.

What I tried is not working even this

var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, "Name"));
claims.Add(new Claim(ClaimTypes.Email, "[email protected]"));
var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);

var ctx = HttpContext.GetOwinContext();
var authenticationManager = ctx.Authentication;
authenticationManager.SignIn(identity);

I really don't know what to do but I want to be logged in the system using my own database, the has only username and password

5
  • I am pretty sure Forms Authentication will continue working. Commented Feb 9, 2014 at 12:29
  • Sorry, but when i use FormsAuthentication.SetAuthCookie it didn't make the Request.IsAuthenticated true Commented Feb 9, 2014 at 12:37
  • Do you have <authentication mode="Forms"> in your web.config? Commented Feb 9, 2014 at 14:28
  • i do have it in my web.config is there is anyway that i can use this new authentication? Commented Feb 9, 2014 at 14:43
  • Well you can implement your own provider but I believe it should be possible to run FormsAuthentication with MVC5 Commented Feb 9, 2014 at 18:25

2 Answers 2

3

Try this, I hope it will help you.

    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            var user = MyViewModels.checkUser(model.UserName, model.Password);
            if (user!=null)
            {
                SignInAsync();
                return RedirectToAction("Welcome");
            }
            else
            {
                ModelState.AddModelError("", "Invalid username or password.");
            }
        }
        return View(model);
    }

    private void SignInAsync()
    {
        var claims = new List<Claim>();
        claims.Add(new Claim(ClaimTypes.Name, "UserName"));
        claims.Add(new Claim(ClaimTypes.Email, "[email protected]"));
        var id = new ClaimsIdentity(claims,
                                    DefaultAuthenticationTypes.ApplicationCookie);

        var ctx = Request.GetOwinContext();
        var authenticationManager = ctx.Authentication;
        authenticationManager.SignIn(id);
    }

    [Authorize]
    public ActionResult Welcome()
    {
        return View();
    }

If you add [Authorize] attribute in the action, then it will redirect only the user name and password is authorize

Function to get user name and password from database

    public static UserTable checkUser(string userName, string password)
    {
        DemoEntities db = new DemoEntities();
        var query = (from u in db.UserTables
                     where u.UserName == userName && u.Password == password
                     select u).FirstOrDefault();
        if(query!=null)
            return query;
        else
            return null;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Look at this post to see how set up a bare bones OWIN authentication with your own membership system.

MVC 5 Forms Auth

1 Comment

Sorry, but this didn't help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.