使用ASP.NET MVC3+EF+Jquery製作文字直播系統(二)

來源:互聯網
上載者:User
實現登入

這一篇,簡簡單單的把背景登入功能實現,沒有什麼技術含量

首先在LiveText.WebUI項目中的Model檔案夾中添加一個LogOnViewModel類,代碼如下:

public class LogOnViewModel{    [Required(ErrorMessage = "不可為空")]    public string UserName { get; set; }    [Required(ErrorMessage = "不可為空")]    [DataType(DataType.Password)]    public string Password { get; set; }}

第二,添加一個AccountController,在Scaffolding選項裡面Template選擇Empty controller,如所示:

AccountController中的代碼也很簡單,主要是一個登入的action和退出的action,下面是具體代碼:

public class AccountController : Controller    {        private LiveTextDbContext context = new LiveTextDbContext();        //        // GET: /Account/LogOn        public ActionResult LogOn()        {            return View();        }        //        // POST: /Account/LogOn        [HttpPost]        public ActionResult LogOn(LogOnViewModel model)        {            if (ModelState.IsValid)            {                if (context.Users.Any(u => u.UserName == model.UserName && u.Password == model.Password))                {                    FormsAuthentication.SetAuthCookie(model.UserName, false);                    return View("~/Views/Admin/Index.cshtml");                }                else                {                    ModelState.AddModelError("", "使用者名稱或密碼不正確");                }            }            return View(model);        }        public ActionResult LogOff()        {            FormsAuthentication.SignOut();            return View("LogOn");        }

在LogOn action中,如果使用者提供的使用者名稱和密碼正確,就跳轉到Index.cshtml。Index.cshtml在View檔案夾裡的Admin檔案夾中,Admin是我建立的檔案夾。

第三,在LogOn上右擊,選擇AddView,預設選項即可,添加之後,在View檔案夾裡自動產生Account檔案夾,以及Account檔案夾裡LogOn.cshtml。

第四,在LogOn.cshtml,我在網上隨便找了個模板加上了,具體代碼可以下載原始碼看一下。下面我只貼出關鍵性的代碼:

@Html.EditorFor(u => u.UserName)@Html.ValidationMessageFor(u => u.UserName)
@Html.EditorFor(u => u.Password)@Html.ValidationMessageFor(u => u.Password)

下面讓我們看一下啟動並執行效果吧:

登入頁面

輸入使用者名稱:admin 密碼:admin登入成功

原始碼:http://files.cnblogs.com/nianming/LiveText20111020.rar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.