locked
How to overcome an "object"non accessible error in the page form? RRS feed

  • Question

  • User-578960655 posted

    I'm currently ondoing Login form for my project with specified database model classes. I have stated the Login controller with no errors but i receive error description list as below:


    Severity Code Description Project File Line Suppression State
    Error CS1061 'ClaimsPrincipal' does not contain a definition for 'email' and no accessible extension method 'email' accepting a first argument of type 'ClaimsPrincipal' could be found (are you missing a using directive or an assembly reference?) "project"D:\..\..\..\Login.cshtml 14 Active.
    Severity Code Description Project File Line Suppression State
    Error CS1061 'ClaimsPrincipal' does not contain a definition for 'password' and no accessible extension method 'password' accepting a first argument of type 'ClaimsPrincipal' could be found (are you missing a using directive or an assembly reference?)"project"D:\..\..\..\Login.cshtml  19 Active.

    I also received warning description referring to my Register class on the list such as:
    Severity Code Description Project File Line Suppression State
    Warning CS0108 'RegisterModel.User' hides inherited member 'PageModel.User'. Use the new keyword if hiding was intended. "project"D:\..\..\Pages\Register.cshtml.cs 27 Active.

    I am not sure where the error comes, but i think there is an error with the User model on the database and it also prevents me to build the project.

    User.cs

    public class User
        {
            [Key]
            public int UserID { 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; }
        }

    login.cshtml

    <h2>Login</h2>
    <hr />
    <div class="row">
        <div class="col-md-4">
            <form method="post">
                <div class="form-group">
                    <label asp-for="User.email" class="control-label"></label>
                    <input asp-for="User.email" class="form-control" />
                    <span asp-validation-for="User.email" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="User.password" class="control-label"></label>
                    <input asp-for="User.password" class="form-control" />
                    <span asp-validation-for="User.password" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <input type="submit" value="Register" class="btn btn-default" />
                </div>
            </form>
        </div>
    </div>

    login.cshtml.cs

     public class LoginModel : PageModel
        {
            [BindProperty]
            public User user { get; set; }
    
            public string Msg;
    
            private DDACRedoContext db;
    
            public LoginModel(DDACRedoContext _db)
            {
                db = _db;
            }
            public void OnGet()
            {
                user = new User();
            }
    
            public IActionResult OnGetLogout()
            {
                HttpContext.Session.Remove("email");
                return RedirectToPage("Index");
            }
    
            public IActionResult OnPost()
            {
                var acc = login(user.email, user.password);
                if(acc == null)
                {
                    Msg = "Invalid";
                    return Page();
                }
                else
                {
                    HttpContext.Session.SetString("email", acc.email);
                    return RedirectToPage("Dashboard");
                }
            }
    
            private User login (string email, string password)
            {
                var user = db.User.SingleOrDefault(a => a.email.Equals(email));
                if(user != null)
                {
                    //if(BCrypt.Net.BCrypt.Verify(password, user.password))
                    if(string.IsNullOrEmpty(user.password))
                    {
                        return user;
                    }
                }
                return null; 
            }
        }

    Monday, April 27, 2020 3:11 PM

Answers

  • User-578960655 posted

    Hi Sean, yes the issue has been solved. I was mistakenly post the question in different topic thread, but it has the full copies on my other question. I'll leave the link down below. Thank you

    Same question with full discussion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 28, 2020 5:13 PM

All replies

  • User475983607 posted

    This is a Razor Pages question not Web Forms which is the subject of this forum.  Anyway you forgot to add the OnPost the input values.

    public IActionResult OnPost(User user)
    {
    	var acc = login(user.email, user.password);
    	if(acc == null)
    	{
    		Msg = "Invalid";
    		return Page();
    	}
    	else
    	{
    		HttpContext.Session.SetString("email", acc.email);
    		return RedirectToPage("Dashboard");
    	}
    }

    As recommended in your other post, you should use modern security patterns.  

    Monday, April 27, 2020 3:25 PM
  • User-578960655 posted

    Hi, thank you for your response. I will verify from the Razor Page thread for further assistance.

    Monday, April 27, 2020 3:28 PM
  • User-1330468790 posted

    Hi emirryhn,

     

    Hope your problem has been solved and thank you for understanding.

    Anyway, if you find that the answer does solve your issues, I suggest you could mark the answer.

    This will help other people who faces the same issue to find the right answer faster in case he/she finds this thread.

     

    Best regards,

    Sean

    Tuesday, April 28, 2020 11:28 AM
  • User-578960655 posted

    Hi Sean, yes the issue has been solved. I was mistakenly post the question in different topic thread, but it has the full copies on my other question. I'll leave the link down below. Thank you

    Same question with full discussion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 28, 2020 5:13 PM