locked
login page does not want to retrieve data from database RRS feed

  • Question

  • User-578960655 posted

    I'm having trouble during login page process to retrieve data from database. It supposed to redirect to index page after the login phase i done. But instead of redirecting page, it stays on the login page and showing no sign of confirmation. 

    login.cshtml

    <div class="row">
        <div class="col-md-4">
            <form method="post">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <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="Sign In" class="btn btn-default" />
                </div>
            </form>
        </div>
    </div>

    login.cshtml.cs

    namespace RedoAssignment.Pages
    {
        public class loginModel : PageModel
        {
            private readonly RedoAssignment.Models.RedoAssignmentContext _context;
    
            [BindProperty]
                public User User { get; set; }
                public async Task<ActionResult> login(User model)
                {
                    if (ModelState.IsValid)
                    {
                        var isValid = (User.email == "Email" && User.password == "Password");
                        if (!isValid)
                        {
                            ModelState.AddModelError("", "username or password is invalid");
    
                            return Page();
                        }
                        var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
                        identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, User.email));
                        identity.AddClaim(new Claim(ClaimTypes.Name, User.email));
                        var principal = new ClaimsPrincipal(identity);
                        return RedirectToPage("Index");
                    }
                    else
                    {
                        ModelState.AddModelError("", "username or password is blank");
                        return Page();
                    }
                }
    
                public class LoginData
                {
                    [Required]
                    public string email { get; set; }
    
                    [Required, DataType(DataType.Password)]
                    public string password { get; set; }
                }
    
            
        }
    }

    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;}
    
        }

    If there is something i'm missing or additional source may required, i would be glad to know.

    Edit: this error message happens during debugging: 

    1>Pages\login.cshtml.cs(29,45,29,50): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
    1>Pages\login.cshtml.cs(25,70,25,78): warning CS0169: The field 'loginModel._context' is never used
    1>Done building project "RedoAssignment.csproj" -- FAILED.

    Friday, April 24, 2020 7:34 AM

All replies

  • User753101303 posted

    Hi,

    What if naming your method OnPost rather than Login? You tried to see if this method is called?

    Else don't use async if you don't have any await in your method.

    Friday, April 24, 2020 9:14 AM
  • User-578960655 posted

    Hi, thank you for your response,

    If the async method is given, what would be the EntityState to retrieve data from database?

    Friday, April 24, 2020 2:14 PM
  • User475983607 posted

    Your not following Razor Page standards.  As recommended above, change login to OnPost.  

    https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/razor-pages-start?view=aspnetcore-3.1&tabs=visual-studio

                public void OnPost(User model)
                {
                    if (ModelState.IsValid)
                    {
                        var isValid = (User.email == "Email" && User.password == "Password");
                        if (!isValid)
                        {
                            ModelState.AddModelError("", "username or password is invalid");
    
                            return Page();
                        }
                        var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme, ClaimTypes.Name, ClaimTypes.Role);
                        identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, User.email));
                        identity.AddClaim(new Claim(ClaimTypes.Name, User.email));
                        var principal = new ClaimsPrincipal(identity);
                        return RedirectToPage("Index");
                    }
                    else
                    {
                        ModelState.AddModelError("", "username or password is blank");
                        return Page();
                    }
                }

    Friday, April 24, 2020 3:00 PM