Redirect to new page after successful Login in ASP.Net MVC

Last Reply 11 months ago By dharmendr

Posted 11 months ago

Authentication is getting done from sql, but the error message is getting redirected to different action. i am not able to perform authentication for this on the same page, as i am using user id in dropdown and password in textbox

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            SectionLogin Sl = new SectionLogin();
            Sl.UserList = GetUser();
            return View(Sl);

        }       

        [HttpPost]
        public ActionResult Login(SectionLogin Ls)
        {
            Ls.UserList = GetUser();
            using (LoginEntities dbu = new LoginEntities())
            {
                //var db = Ls.UserList.Where(model => model.Text == Ls.Section_name.ToString() && model.Value == Ls.Section_pwd).FirstOrDefault();
                var db = dbu.SectionLogins.Where(model => model.Section_name == Ls.Section_name && model.Section_pwd == Ls.Section_pwd).FirstOrDefault();
                if (db == null)
                {                  
                    TempData["Message"] = "Wrong User name or pwd";
                    //return View("Index");
                }
                else
                {
                    TempData["Message"] = "Successfull";
                }
            }
                
            return View();
        }

        private static List<SelectListItem> GetUser()
        {
            string str = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;
            List<SelectListItem> List = new List<SelectListItem>();
            using(SqlConnection con=new SqlConnection(str))
            {
                string query = "select Section_name from [CPSEdb].[dbo].[SectionLogin]";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection=con;
                    con.Open();
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        while (rd.Read())
                        {
                            List.Add(new SelectListItem
                            {
                                Text = rd["Section_name"].ToString(),
                                Value = rd["Section_name"].ToString()
                            });
                        }
                    }
                    con.Close();
                }
            }
            return List;
        }
    }

 

@model CPSEDetail.Models.SectionLogin
@{
    ViewBag.Title = "Index";
}

@using (Html.BeginForm("Login", "Home", FormMethod.Post))
{
    @Html.DropDownListFor(model => model.Section_name, Model.UserList, "--Select")
    @Html.TextBoxFor(model => model.Section_pwd)
    <input type="submit" value="submit" />
}

 

@{
    ViewBag.Title = "Login";
}
@if (TempData["Message"] != null)
{
    <p>@TempData["Message"]</p>
}
<h2>Login</h2>

 

namespace CPSEDetail.Models
{
    using System;
    using System.Collections.Generic;
    using System.Web.Mvc;

    public partial class SectionLogin
    {
        public string Section_name { get; set; }
        public string Section_pwd { get; set; }
        public List<SelectListItem> UserList {get;set;}        
    }
}

 

Posted 11 months ago

Hi sunnyk21,

Check this example. Now please take its reference and correct your code.

Model

public class SectionLogin
{
    public string Section_name { get; set; }
    public string Section_pwd { get; set; }
    public List<SelectListItem> UserList { get; set; }
}

Namespaces

using System.Configuration;
using System.Data.SqlClient;

Controller

public class HomeController : Controller
{
    // GET: /Home/
    public ActionResult Index()
    {
        SectionLogin Sl = new SectionLogin();
        Sl.UserList = GetUser();
        return View(Sl);
    }

    [HttpPost]
    public ActionResult Index(SectionLogin Ls)
    {
        Ls.UserList = GetUser();
        using (LoginEntities dbu = new LoginEntities())
        {
            var db = dbu.Users.Where(model => model.UserName == Ls.Section_name && model.Password == Ls.Section_pwd).FirstOrDefault();
            if (db == null)
            {
                TempData["Message"] = "Wrong User name or pwd";
                return View(Ls);
            }
            else
            {
                TempData["Message"] = "Successfull";
                return View("Section");
            }
        }
    }

    private static List<SelectListItem> GetUser()
    {
        string str = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;
        List<SelectListItem> List = new List<SelectListItem>();
        using (SqlConnection con = new SqlConnection(str))
        {
            string query = "select UserName from [User]";
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader rd = cmd.ExecuteReader())
                {
                    while (rd.Read())
                    {
                        List.Add(new SelectListItem
                        {
                            Text = rd["UserName"].ToString(),
                            Value = rd["UserName"].ToString()

                        });
                    }
                }
                con.Close();

            }
        }
        return List;
    }
}

View

Index

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<_855589_Login_DropDownList.Models.SectionLogin>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Index</title>
</head>
<body>
    <%using (Html.BeginForm("Index", "Home", FormMethod.Post))
      {%>
    <%:Html.DropDownListFor(model => model.Section_name,Model.UserList, "--Select Section--")%>
    <%:Html.TextBoxFor(model => model.Section_pwd) %>
    <input type="submit" value="submit" />
    <%}  %>
    <%if (TempData["Message"] != null)
      { %>
    <p><%= TempData["Message"]%></p>
    <%} %>
</body>
</html>

Section

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Section</title>
</head>
<body>
    <div>
        <%if (TempData["Message"] != null)
          { %>
        <p><%= TempData["Message"]%></p>
        <%} %>
    </div>
</body>
</html>

Screenshot