MVC模式的登录页面 [英] Login page in mvc pattern

查看:345
本文介绍了MVC模式的登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有2个文本框的用户名和密码的登录页面.我想根据页面中输入的用户名和密码检查数据库中是否存在用户名.我使用MVC模式..plz help

I have a login page with 2 textboxes username and password.I want to check if the username exists in the database based on the user entered username and password in the page.Im using MVC pattern..plz help

推荐答案

您将按照以下方式进行操作

假设您的页面是通过调用名为Logon的操作方法显示的,则可以编写另一种仅限于HttpPost
的Logon操作方法.
You would do something along these lines

Assuming your page was shown from a call to an action method named Logon, you could code another Logon action method that is limited to HttpPost

[HttpPost]
public ActionResult LogOn(LogOnModel model)
{
    if (ModelState.IsValid)
    {
        if (MembershipService.ValidateUser(model.UserName, model.Password))
        {
            AuthenticationService.SignIn(model.UserName, model.RememberMe);
            return RedirectToAction("Index", "Home");
        }
        else
        {
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
        }
    }
    // If we got this far, something failed - redisplay form with model
    return View(model);
}



LogOnModel的代码将是这样的...



The code for LogOnModel would be something like this...

public class LogOnModel
{
    [Required]
    [DisplayName("User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [DisplayName("Password")]
    public string Password { get; set; }

    [DisplayName("Remember me?")]
    public bool RememberMe { get; set; }
}



此示例使用成员资格服务来检查用户,但是如果需要,您可以轻松地在此处滚动自己的用户



This example is using the membership service to check for the user, but you could easily roll your own here if you wanted


这篇关于MVC模式的登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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