locked
login feature of Website. RRS feed

  • Question

  • User1421620300 posted

    Can someone help me with correcting the issue that i am having with my login process to my website? I am currently building on a project and adding certain features to make it more dynamic. Just recently i added bootstrap snippet code to view, but previously was working with some instruction on how to make login process work! So now i have the template with the back-end code to the server that i want to use. But only thing is that when i compile there is no run time error or anything. So it make it difficult to even mange what is going on. When i login the screen just snaps with no error. I am using SSMS database for login credentials could someone go over my code and tell me where their is problem. Thanks, Markus33

    Login.cshtml
    https://ibb.co/19rxg11

    AccountsController
    https://ibb.co/L5ZFK31

    Web.config
    https://ibb.co/MDcFY0D

    Tuesday, April 14, 2020 2:10 PM

Answers

  • User1535942433 posted

    Hi Markus33,

    Accroding to your description and codes,as far a I think,there are some problems with your codes,for example:

    1.The View is not calling the Verify() action in the controller.

    2.ConnectionStrings name is wrong.

    More details,you could refer to below codes:

    View:

    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
        <script src="~/Scripts/jquery-3.0.0.min.js"></script>
        <script src="~/Scripts/bootstrap.min.js"></script>
        <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
    </head>
    <body>
        @using (Html.BeginForm("Verify", "Website"))
        {
            <div class="sidenav">
                <div class="=login-main-text">
                    <h2>Application<br />Login Page</h2>
                    <p>Login or register from here to access.</p>
                </div>
            </div>
            <div class="main">
                <div class="col-md-6 col-sm-12">
                    <div class="login-form">
                        <div class="form-group">
                            <label>User Name</label>
                            <input name="name" type="text" class="form-control" placeholder="User Name" />
                        </div>
                        <div class="form-group">
                            <label>User Name</label>
                            <input name="password" type="text" class="form-control" placeholder="Password" />
                        </div>
                        <button type="submit" class="btn btn-black">Login</button>
                        <button type="submit" class="btn btn-secondary">Register</button>
                    </div>
                </div>
            </div>
        }

    Controller:

    [AllowAnonymous]
            [HttpGet]
            public ActionResult Index()
            {
                return View();
            }
            [HttpPost]
            public ActionResult Verify(Account acc)
            {
                string cs = ConfigurationManager.ConnectionStrings["CrudApp"].ConnectionString;
                SqlConnection connection = new SqlConnection(cs);
                connection.Open();
                string commandText = "SELECT * FROM login WHERE username=@Username AND password=@Password";
                SqlCommand command = new SqlCommand(commandText, connection);
                command.Parameters.AddWithValue("@Username", acc.name);
                command.Parameters.AddWithValue("@Password", acc.password);
                SqlDataReader dr = command.ExecuteReader();
                if (dr.Read())
                {
                    connection.Close();
                    TempData["Message"] = "Login failed.User name or password supplied doesn't exist.";
                }
                else
                {
                    connection.Close();
                    Console.Write(2);
                }
                return View();
    
            }
            public class Account
            {
                public string name { get; set; }
                public string password { get; set; }
            }

    Webconfig:

     <connectionStrings>
        <add name="CrudApp" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=aspnet-TestApplicationWithDatabase-20190820030542;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" providerName="System.Data.SqlClient" />
      </connectionStrings>

    More details,you could refer to below article:

    https://stackoverflow.com/questions/38807926/validate-login-form-asp-net-mvc

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 15, 2020 6:58 AM