HttpPost

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>

时间: 2024-10-14 16:44:18

HttpPost的相关文章

Android数据与服务器交互的GET,POST,HTTPGET,HTTPPOST的使用

Android有这几种方式,可以提交数据到服务器,他们是怎么使用的呢,这里我们来探讨一下. 这里的例子用的都是提交客户端的用户名及密码,同时本节用到的StreamTools.readInputStream(is);作用是把输入流转化为字符串,是一个公共类.我在前面介绍过了.http://www.cnblogs.com/fengtengfei/p/3969520.html,这里就不在重复的说明了. 第一种:GET 关键部分是: 首先我们用URL包装访问的路径,由于是get请求,在学习javaWEB

ios构造httpPost头结构

ios构造httpPost头结构 by 伍雪颖 NSString* urlStr = @"; NSURL* url = [NSURL URLWithString:urlStr]; NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; [request addValue:@"application/x-www-form

访问taotao-portal 中controller中返回taotaoresult 测试httppost方法 出现406错误

方案:1.检查jackson包是否存在 @controller @RequestMapping(value = "/httpclient/post",method=RequestMethod.POST) @ResponseBody public TaotaoResult testPost(String username,String password){ system.out.println( "username" + username + "/t pas

HTTPPost/AFNetWorking/JSONModel/NSPredicate

一.HTTPPost================================================ 1. POST方式发送请求 HTTP协议下默认数据发送请求方法是GET方式,若需要使用POST方法,则需要对发送的请求也就是request对象,进行属性设置. 步骤如下: > 要发送的请求对象,需要使用可变请求对象 [[NSMutableURLRequest alloc] initWithURL:] 此时创建的请求对象,其相关属性是可以进行设置的,通过NSURLRequest创建

ASP.NEt MVC5--为列表页添加HttpPost方法。

我们都知道,get方法,主要用来显示数据,检索之类,对数据一般不会修改.这次我做一个测试为Index方法写一个post方法. 1.get方式 public ActionResult Index(string searchString) { var movies = from m in db.Movies select m; if (!string.IsNullOrEmpty(searchString)) { movies = movies.Where(s => s.Title.Contains(

XML Web Service:HTTP-GET, HTTP-POST and SOAP的比较

XML Web Service:HTTP-GET, HTTP-POST and SOAP的比较 XML Web Service支持三种协议来与用户交流数据.这三种协议分别是: 1. SOAP:Simple Object Access Protocol 2.  HTTP-GET 3.   HTTP-POST 1.首先我们先来理解一下这三者的大概定义. 在这三种协议中,SOAP是XML Web Service最常用到的连接协议.与HTTP相比,SOAP显的更为复杂,但却拥有更强的接受能力.SOAP是

Java HttpPost

Java HttpPost 最近写项目的时候,需要用到java获得一个服务器上数据库的内容. 在连接服务器数据库时需要使用以上包中的类,使用json格式传送 1 public static String Register(String username,String password,int operator) throws Exception 2 { 3 String returnString = "Failed"; 4 String []urlString = {"htt

HttpPost,MD5,Base64等接口调用中会用到的方法

HttpPost调用方法: //url: 接口地址,param:类似key1=value1&key2=value2字符串 public static string WebRequestPost(string url, string param) { string strResult = ""; HttpWebRequest request = null; HttpWebResponse response = null; Stream stream = null; StreamR

HttpPost、HttpPut、HttpGet、HttpDelete区别

http://www.cnblogs.com/Leo_wl/p/3528925.html(牛掰) 在C#代码中,我们经常看到代码里的方法上面有[HttpPost].[HttpPut].[HttpGet].[HttpDelete],那么它们究竟是干什么用的,以及区别,作如下归纳总结: 在HTTP中,PUT被定义为idempotent的方法,POST则不是,这是一个很重要的区别. 如果一个方法重复执行多次,产生的效果是一样的,那就是idempotent的. 举一个简单的例子,假如有一个博客系统提供一