ASP.NET MVC CORE POST GRES LOGIN PAGE

Anonymous
2023-05-26T10:45:25.78+00:00

I completed a mini project in ASP.NET MVC CORE PROSGRE, I want to deploy the same only login page is pending.

I want to create a simple login page , as below.

Please suggest github link here

https://github.com/KalyanAllam/StudentInformationPostgres

 string uid = TextBox1.Text;
            string pass = TextBox2.Text;
            string outpass;


            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            var con = new NpgsqlConnection(CS);
            con.Open();
            NpgsqlCommand cmd = new NpgsqlCommand("SELECT COUNT(*) FROM userdata where email=@Email and password=@Password", con);
 
            cmd.Parameters.AddWithValue("Email", uid);
            cmd.Parameters.AddWithValue("Password", pass);

      
            Int64 Username = (Int64)cmd.ExecuteScalar();

            if (Username == 1)
            {
                Session["fromlogin"] = "Y";
                Response.Redirect("Main.aspx");
            }
            else
            { Label4.Text = "Invalid Credentials.!!"; }
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,815 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. AgaveJoe 30,031 Reputation points
    2023-05-26T13:00:14.1733333+00:00

    Your post is unclear. You said you're working in an MVC Core project but the code you shared is Web Forms.

    If you are working on an ASP.NET Core project and you are looking for authentication/authorization then read the tutorial.

    Use cookie authentication without ASP.NET Core Identity

    0 comments No comments

  2. Anonymous
    2023-05-26T13:18:48.0166667+00:00

    I have login page in asp.net want to convert to mvc core want to know how to do it.


  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

  4. Zhi Lv - MSFT 33,191 Reputation points Microsoft External Staff
    2023-05-29T03:04:36.13+00:00

    Hi @Anonymous

    I want to deploy the same only login page is pending. I want to create a simple login page , as below.

    First, about the Login page, it seems that you have already solved the problem. In your code, after clicking the submit button, it will submit the LoginViewModel to the Index (Post) method. Then, you can get the entered value from the LoginViewModel.

    User's image

    More detail information about CRUD operations in Asp.net core MVC, see Tutorial: Implement CRUD Functionality - ASP.NET MVC with EF Core.

    I have one quick question , I see Home button even on login page , which I dont want to see , I want to see it once I login, how do I eliminate it , I guess there is some kind of partial view.

    The issue relates the _Layout.cshtml page.

    In the _Layout.cshtml page, you can check if user is authenticated or not via the User.Identity.IsAuthenticated property, if user is authenticated, display the relates navigation button, otherwise not.

    Note: By using the method, in all the pages which using the _Layout.cshtml page, it will show the navigation links based on the user IsAuthenticated property.

                <div class="container-fluid">
                      @if (User.Identity.IsAuthenticated)
                      {
                            <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">SISPostgres</a>
                            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                                    aria-expanded="false" aria-label="Toggle navigation">
                                <span class="navbar-toggler-icon"></span>
                            </button>
                      }                  
                    <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                        <ul class="navbar-nav flex-grow-1"> 
                            @if (User.Identity.IsAuthenticated)
                            {
                                <li class="nav-item">
                                    <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
                                </li>
                            }
                            <li class="nav-item" style="margin-left: auto">
                                <a class="nav-link text-dark" asp-area="" asp-controller="Login" asp-action="Index">Logout</a>
                            </li>
                        </ul>
                    </div>
                </div>
    

    The result as below:

    User's image

    If you just want to remove the navigation links from the Login page. Instead of using session, you can try to use ViewData or ViewBag to store the fromlogin variable.

    Code like this: in this sample, I use ViewData to store the isDisplay.

            <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
                <div class="container-fluid">
                    @if ((ViewData["isDisplay"] !=null && (bool)ViewData["isDisplay"]) || (ViewData["isDisplay"] == null))
                    {
                            <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">SISPostgres</a>
                            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                                    aria-expanded="false" aria-label="Toggle navigation">
                                <span class="navbar-toggler-icon"></span>
                            </button>
                      }                  
                    <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                        <ul class="navbar-nav flex-grow-1"> 
                            @if ((ViewData["isDisplay"] !=null && (bool)ViewData["isDisplay"]) || (ViewData["isDisplay"] == null))
                            {
                                <li class="nav-item">
                                    <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
                                </li>
                            }
                            <li class="nav-item" style="margin-left: auto">
                                <a class="nav-link text-dark" asp-area="" asp-controller="Login" asp-action="Index">Logout</a>
                            </li>
                        </ul>
                    </div>
                </div>
            </nav>
    

    In the Login Controller: set the ViewData["isDisplay"] value.

            public IActionResult Index()
            {
                ViewData["isDisplay"] =false;
                return View();
            }
    
    

    Then the result as below: Note: using the above code, to remove the navigation link, we just need to set the ViewData to false in the related action method. There is no need to set the ViewData to true in other actions/pages.

    User's image

    Besides, in asp.net core, to use session state management, you can refer to Session and state management in ASP.NET Core.

    Finally, please avoid posting multiple questions in a single thread. Because, to ensure that you receive a clear and helpful answer to your questions, it is important to post them individually. When you have multiple questions, each with its own distinct scenario, it can make it more difficult for the community to understand the problem and provide an accurate solution. For this reason, we recommend that you create separate threads for each of your questions. This way, the community can give you their undivided attention and provide the best possible response. Thanks for your understanding.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.