博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpPost
阅读量:5072 次
发布时间:2019-06-12

本文共 2875 字,大约阅读时间需要 9 分钟。

Create  Model LoginPageViewModel.cs

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CustomSecurityExampleMVC30.Models

{
  public class LoginPageViewModel
  {
    public string Username { get; set; }
    public string Password { get; set; }
    public string ErrorMessage { get; set; }
  }
}

Create Account/Login.cshtml View 

@model CustomSecurityExampleMVC30.Models.LoginPageViewModel

@{

  ViewBag.Title = "Login";
  Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
  <fieldset style="width: 280px;">
  <legend>Login</legend>
  <p>
    <label>
    username</label>
    @Html.TextBoxFor(x => x.Username)
  </p>
  <p>
    <label>
    Password</label>
    @Html.TextBoxFor(x => x.Password)
  </p>
  <p>
    <input type="submit" value="login" />
  </p>
  </fieldset>
}

Create AccountController.cs Controller

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using CustomSecurityExampleMVC30.Models;
using System.Web.Mvc;

namespace CustomSecurityExampleMVC30.Controllers

{
  public class AccountController : BaseController
  {
    public ActionResult Login()
    {
      var vm = new LoginPageViewModel
      {
        Username = "Username",
        Password = "Password"
      };

      return View(vm);

    }

    [HttpPost]

    public ActionResult Login(LoginPageViewModel viewModel)
    {
      if (string.IsNullOrEmpty(viewModel.Username)|| string.IsNullOrEmpty(viewModel.Password))
      {
        viewModel.ErrorMessage = "Please provide your username and password";
        return View(viewModel);
      }

      SimpleSessionPersister.Username = viewModel.Username;

      return RedirectToAction("Index", "Home");

    }
  }
}

Create IndexPageViewModel.cs Model

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CustomSecurityExampleMVC30.Models

{
  public class IndexPageViewModel
  {
    public string CurrentUsername { get; set; }
    //51aspx.com下载
  }
}

 

Create View Index.cshtml

@model CustomSecurityExampleMVC30.Models.IndexPageViewModel

@{

  ViewBag.Title = "Index";
  Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<h3>Welcome, @Model.CurrentUsername!</h3>

 

Create Controller HomeController.cs

using CustomSecurityExampleMVC30.Models;

using System.Web.Mvc;

namespace CustomSecurityExampleMVC30.Controllers

{
  public class HomeController : BaseController
  {
    [Authorize]
    public ActionResult Index()
    {
      return View(
        new IndexPageViewModel
        {
          CurrentUsername = this.User.Identity.Name

        }

      );//51aspx.com下载

    }
  }
}

 

set Account/Login.cshtml as start page in web.config

 

<system.web>

  <authentication mode="Forms">

    <forms loginUrl="~/Account/Login" timeout="2880" />
  </authentication>

</system.web>

 

转载于:https://www.cnblogs.com/ganting/p/4562210.html

你可能感兴趣的文章
pig自定义UDF
查看>>
spring security 11种过滤器介绍
查看>>
代码实现导航栏分割线
查看>>
大数据学习系列(8)-- WordCount+Block+Split+Shuffle+Map+Reduce技术详解
查看>>
【AS3代码】播放FLV视频流的三步骤!
查看>>
枚举的使用
查看>>
luogu4849 寻找宝藏 (cdq分治+dp)
查看>>
日志框架--(一)基础篇
查看>>
关于源程序到可运行程序的过程
查看>>
转载:mysql数据库密码忘记找回方法
查看>>
scratch少儿编程第一季——06、人在江湖混,没有背景怎么行。
查看>>
C# Async与Await的使用
查看>>
Mysql性能调优
查看>>
iOS基础-UIKit框架-多控制器管理-实例:qq界面框架
查看>>
自定义tabbar(纯代码)
查看>>
小程序底部导航栏
查看>>
poj1611 简单并查集
查看>>
Ubuntu 14.04下安装CUDA8.0
查看>>
跨平台开发 -- C# 使用 C/C++ 生成的动态链接库
查看>>
C# BS消息推送 SignalR介绍(一)
查看>>