如何在mvc中创建登录页面以访问索引 [英] how to create a login page in mvc to access index

查看:71
本文介绍了如何在mvc中创建登录页面以访问索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,mVC的新手想要为我的应用程序创建一个登录页面。我可以做一个简单的。但是我想看到一个错误消息,当输入错误的用户名和密码时,如用户未找到





这里是我的控制器代码



 使用 MvcApplication1.Models; 
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.Mvc;
使用 System.Web.Security;

命名空间 MvcApplication1.Controllers
{
public class LoginController:Controller
{
//
// GET:/ Login /

public ActionResult Index()
{
return 查看() ;
}
public ActionResult Login()
{
return View();
}

[HttpPost]
public ActionResult登录(客户d)
{
使用(CustomerDataEntities oe = new CustomerDataEntities())
{
var user = oe.Customers.Where(a = > a.UserName.Equals(d.UserName)& & a.Password.Equals(d.Password))。FirstOrDefault();
return RedirectToAction( Index Home);
}

}
}

}

解决方案

< blockquote>如果UserName和Password在Customer表中不匹配,则FirstOrDefault方法将返回null作为默认值。因此,您可以使用if条件来验证用户是否为空。

示例代码如下所示:

 public ActionResult Login(Customer d) 
{
使用(CustomerDataEntities oe = new CustomerDataEntities())
{
var user = oe.Customers.Where(a => a.UserName.Equals(d.UserName) )&&
a.Password.Equals(d.Password))。FirstOrDefault();
if(user == null)
{
//如果UserName和Password不正确,则重定向到Login页面
返回RedirectToAction(Login,Home);
}
else
{
//如果UserName和Password正确,则重定向到Index页面
返回RedirectToAction(Index,Home);
}
}
}



如有任何问题请告诉我们。


控制器 -

  public  ActionResult登录(客户d)
{
使用(CustomerDataEntities oe = new CustomerDataEntities())
{
var user = oe.Customers.FirstOrDefault(a = > a.UserName.Equals(d .UserName)&& a.Password.Equals(d.Password));
if (user == null
{
TempData [ ErrorMessage] = 无效的用户名或密码。
return RedirectToAction( 登录 Home< /跨度>);
}
else
{
return RedirectToAction( 索引 主页);
}
}
}



查看 - 将此代码放在要显示错误消息的任何位置

 @ {
if(TempData [ErrorMessage]!= null)
{
< div id = errorMsg < span class =code-attribute> style = border:solid 1px red; padding:50px 10px ; color:red >
@TempData [ErrorMessage]
< / div >
}
}





-KR


hey m just new to mVC want to create a login page for my application.i can do the simple one. but i want to see a error messege there when putting wrong username and password like user not found


here is my controller code

using MvcApplication1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace MvcApplication1.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Login()
        {
            return View();
        }

       [HttpPost]
 public ActionResult Login(Customer  d)
{
    using (CustomerDataEntities oe = new CustomerDataEntities())
    {
        var user = oe.Customers.Where(a => a.UserName.Equals(d.UserName) && a.Password.Equals(d.Password)).FirstOrDefault();
        return RedirectToAction("Index","Home");
    }
           
}
        }

    }

解决方案

FirstOrDefault method will return null as default value if UserName and Password does not match in Customer table. So you can use "if" condition to verify user is null or not.
Sample code shown below:

public ActionResult Login(Customer d)
        {
            using (CustomerDataEntities oe = new CustomerDataEntities())
            {
                var user = oe.Customers.Where(a => a.UserName.Equals(d.UserName) &&
                                                  a.Password.Equals(d.Password)).FirstOrDefault();
                if (user == null)
                {
                    // If UserName and Password is incorrect then redirect to Login page
                    return RedirectToAction("Login", "Home");
                }
                else
                {
                    // If UserName and Password is correct then redirect to Index page
                    return RedirectToAction("Index", "Home");
                }
            }
        }


In case of any issue let us know.


Controller-

public ActionResult Login(Customer d)
        {
            using (CustomerDataEntities oe = new CustomerDataEntities())
            {
                var user = oe.Customers.FirstOrDefault(a => a.UserName.Equals(d.UserName) && a.Password.Equals(d.Password));
                if (user == null)
                {
                    TempData["ErrorMessage"] = "Invalid user name or password."
                    return RedirectToAction("Login", "Home");
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
        }


View- Place this code wherever you want to display the error message

@{
    if(TempData["ErrorMessage"] != null)
    {
        <div id="errorMsg" style="border: solid 1px red; padding: 50px 10px; color:red">
            @TempData["ErrorMessage"]
        </div>
    }
}



-KR


这篇关于如何在mvc中创建登录页面以访问索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆