Authentication and login page - how can I go through authentication?

I read: FormsAuthentication with Razor does not work , but may get some errors from the answer.

using  System.Web.Security;

[HttpPost]
public ActionResult Index(LoginModel model, string name, string pw)
{
    if (ModelState.IsValid && Membership.ValidateUser(name, pw))
    {
        if (!String.IsNullOrEmpty(pw) && !String.IsNullOrEmpty(name))
        {
            try 
            {
                var db = new UsersDBContext();
                var Name = (from Users in db.Users
                            where Users.Name == name && Users.Password == pw
                            select Users.CustomerId).Single();

                if (Name != 0)
                {
                    string myIDString = Name.ToString();
                    Session["myID"] = myIDString;
                    return Redirect("/LogModels/Index");
                }
            }
            catch (DbEntityValidationException ex)
            {
                // Retrieve the error messages as a list of strings.
                var errorMessages = ex.EntityValidationErrors
                        .SelectMany(x => x.ValidationErrors)
                        .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                // Throw a new DbEntityValidationException with the improved exception message.
                throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
            }
        }
        else
        {
            //Add alert -> password does not exist/is wrong etc.
        }
    }
    return Redirect("/");
}

The problem is what I'm trying to add

&& Membership.ValidateUser(model.UserName, model.Password)

I get the following error.

Membership does not exist in the current context.

I am new to this, so please explain it in a very simple way, if possible.

Thanks in advance.

EDIT

LoginModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace WebPortalMVC.Models
{
    [Table("Users")]
    public class UsersModel
    {
        [Key]
        public int Id { get; set; }

        [Display(Name = "Name")]
        public string Name { get; set; }

        //[DataType(DataType.Password)]
        [Display(Name = "Password")]
        [DataType(DataType.Password)]
        public string Password { get; set; }
        /*
        [Display(Name = "Remember me on this computer")]
        public bool RememberMe { get; set; }
        */
        public int CustomerId { get; set; }
    }  
    public class UsersDBContext : DbContext
    {
        public UsersDBContext() : base("MySqlConnection")
        {
        }

        public DbSet<UsersModel> Users { get; set; }
    }
}

View (not all)

<div id="login">
                <h1>Web Portal</h1>

                @using (Html.BeginForm())
                {
                    <p>
                        @Html.TextBox("name", "", new { type = "email", placeholder = "example@email.com" })
                    </p>
                    <p>
                        @Html.Password("pw",  "", new { type = "password", placeholder = "Password" })
                    </p>
                        <input type="submit" id="loginsubmit" value="Log in" onclick="emptypasswordcheck()" />
                }
            </div>
        </div>

** EDIT 2: ** Added code according to @FreshBM's answer.

+4
source share
1 answer

Membership.ValidateUser System.Web.Security . , , , System.Web.Security.

.

:

ValidateUser, . , pwd:

Membership.ValidateUser(name, pwd)

LoginModel :

using WebPortalMVC.Models;

. , - Resolve : Ctrl + .

MVC:

http://www.codeproject.com/Articles/578374/AplusBeginner-splusTutorialplusonplusCustomplusF

http://www.codeguru.com/csharp/article.php/c18813/Using-Forms-Authentication-in-ASPNET-MVC-Applications.htm

+2

Source: https://habr.com/ru/post/1608303/


All Articles