locked
FormsAuthentication and IsValid method does not exist in current context error RRS feed

  • Question

  • User-578960655 posted

    i'm new with ASP.net web razor page app and i'm currently creating login validation form by taking data from specific database. I'm using FormsAuthentication model to bound directory during log in process. But instead i received red-line on the model. Please help me to solve my issue

    login.cshtml.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using RedoAssignment.Models;
    using Microsoft.EntityFrameworkCore;
    using RedoAssignment.Table;
    using System.Web;
    
    namespace RedoAssignment.Pages
    {
    public class loginModel : PageModel
    {
        public ActionResult Index()
        {
            return Page();
        }
    
        [HttpGet]
        public ActionResult login()
        {
            return Page();
        }
    
        [HttpPost]
        public ActionResult login(Models.RedoAssignmentContext context)
        {
            if (ModelState.IsValid)
                {
                   FormsAuthentication.SetAuthCookie(User.email, User.password); //Error statement
                   return RedirectToAction("Index", "Home");
                 }
    
                else
                {
                    ModelState.AddModelError("", "Login data is incorrect!");
                }
    
            return Page();
        }
    }

    User.cs model

     public class User
    {
        [Key]
        public int customerID { get; set; }
    
        [StringLength(60, MinimumLength = 3)]
        [Required]
        [Display(Name = "Full Name")]
        public string fullName { get; set; }
    
        [Required]
        [Display(Name = "Email")]
        [DataType(DataType.EmailAddress)]
        public string email { get; set; }
    
        [Required]
        [Display(Name = "Password")]
        [DataType(DataType.Password)]
        public string password { get; set; }
    
        [StringLength(20, MinimumLength = 3)]
        [Required]
        [Display(Name = "Contact Number")]
        public string contactNumber { get; set; }
    
        [StringLength(60, MinimumLength = 3)]
        [Required]
        [Display(Name = "Address")]
        public string address { get; set; }
    }

    </div> </div> </div>

    Tuesday, April 21, 2020 8:20 AM

All replies

  • User475983607 posted

    As far as I know, Forms Authentication does not exist in Razor Pages.   Use Cookie Authentication. 

    https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-3.1

    Tuesday, April 21, 2020 12:21 PM
  • User-578960655 posted

    Hi, thank you for your response. I have changed the method by using cookie authentication and it shows no error. However i found another problem where the object from User database does not get called during login process. Instead it returns to index page, it only stays at the same page after pressing the submit button.

    Tuesday, April 21, 2020 3:28 PM
  • User475983607 posted

    Hi, thank you for your response. I have changed the method by using cookie authentication and it shows no error. However i found another problem where the object from User database does not get called during login process. Instead it returns to index page, it only stays at the same page after pressing the submit button.

    If you want a code review and assistance to find the bug then share all relevant source code.  Otherwise, there's not much the community can do for you.

    Tuesday, April 21, 2020 3:57 PM
  • User-17257777 posted

    Hi emirryhn,

    Instead it returns to index page, it only stays at the same page after pressing the submit button.

    Please debug your project. From your description and codes, it seems that the ModelSate is invaild, so it didn't redirect to index page but stay at the current page.

    Best Regards,

    Jiadong Meng

    Wednesday, April 22, 2020 7:34 AM