ASP.NET MVC框架学习#3:账户管理

前言

上一章我们建立了数据库,实现了对业务信息的管理,但是网站的建设中还有一类比较特殊的数据管理,就是对数据的加密以及验证访问,最常用的就是账户的注册,登录,注销,修改等。
为了研究这个问题,本章会在原先网站基础上添加用户注册登录界面。

数据模型

首先,仍然是定义数据模型

Account
变量类型说明
Idint关键值,数据库定位数据用。
UserNamestring用户名
Passwordstring用户密码
ConfirmedPasswordstring第二次验证密码,用户注册时用到,不存入数据库
函数名参数返回类型功能说明
IsValid()bool从数据库中对比用户名和密码
IsExtends()bool判断是否已经存在相同用户名
IsPasswordConfirmed()bool注册密码验证

代码:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

namespace MvcDemo.Models {

	public class Account {
		private MvcDemoContext db = new MvcDemoContext();
		private string AdminName = "admin";
		private string AdminPassword = "admin";

		[Key]
		public int Id { get; set; }

		[Required]
		[DisplayName("用户名")]
		public string UserName { get; set; }

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

		//注册验证密码,不用保存
		[NotMapped]
		[DisplayName("密码确认")]
		[DataType(DataType.Password)]
		public string ConfirmedPassword { get; set; }

		//该方法应从数据库中对比用户名和密码
		public bool IsValid() {

			foreach (var item in db.Accounts) {
				if (item.UserName == this.UserName && item.Password == this.Password) {
					return true;
				}
			}

			//默认管理员密码
			if (this.UserName == AdminName & this.Password == AdminPassword) {
				return true;
			}

			return false;
		}

		//是否是管理员登录
		public bool IsAdministrator() {
			//默认管理员密码
			if (this.UserName == AdminName & this.Password == AdminPassword) {
				return true;
			}
			return false;
		}

		//判断是否已经存在相同用户名
		public bool IsExtends() {

			foreach (var item in db.Accounts) {
				if (item.UserName == this.UserName) {
					return true;
				}
			}

			return false;
		}

		//注册密码验证
		public bool IsPasswordConfirmed() {
			if (this.Password == this.ConfirmedPassword) {
				return true;
			}
			return false;
		}
	}
}

当然还要连接数据库

using System.Data.Entity;

namespace MvcDemo.Models {
	public class MvcDemoContext : DbContext {
		public MvcDemoContext() : base("name = MvcDemoContext") {

		}
		public DbSet<Movie> Movies { get; set; }
		public DbSet<Music> Musics { get; set; }
    //连接账号数据
		public DbSet<Account> Accounts { get; set; }
	}
}

Controller

然后我们新建

~/Controller/AccountController.cs

在这个Controller中,我们定义在什么情况下,使用什么Action。

using MvcDemo.Models;
using System.Web.Mvc;

namespace MvcDemo.Controllers {
	public class AccountController : Controller {

		private MvcDemoContext db = new MvcDemoContext();

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

		//Login
		[HttpPost]
		public ActionResult Login(Account account) {
			if (!ModelState.IsValid) {
				return View();
			}
			if (account.IsAdministrator()) {
				return RedirectToAction("LoginAsAdministrator", "Account");
			}
			if (account.IsValid()) {
				return RedirectToAction("LoginSuccess", "Account");
			}
			return View(account);
		}

		//LoginSuccess
		public ActionResult LoginSuccess() {
			return View();
		}

		//LoginAsAdministrator
		public ActionResult LoginAsAdministrator() {
			return View(db.Accounts);
		}

		//Register
		public ActionResult Register() {
			return View();
		}

		//Register
		[HttpPost]
		public ActionResult Register(Account account) {
			if (!ModelState.IsValid) {
				return View(account);
			}
			if (!account.IsPasswordConfirmed()) {
				return View(account);
			}
			if (account.IsExtends()) {
				return View(account);
			}
			db.Accounts.Add(account);
			db.SaveChanges();
			return RedirectToAction("Index", "Home");
		}
	}
}

View

最后根据Controller的设计,添加相应的View。
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
有了这款基于JavaScript的ASP开源MVC框架,你的asp老项目有可以焕发青春活力了!其实不伦是asp,php,java还是python,任何一种编程语言,只要是开源的,就可以不断更新,获得延续的生命力。 单文件入口。Single entry point. > > 代码和程序的真正分离。A real MVC. > > 模板编译ASP代码。Combine template file to ASP(JScript) code. > > 支持类库扩展以及模板自定义标签扩展。Support Library and Tag extend. > > 支持多种数据库,默认支持ACCESS、MSSQL、MYSQL、SQLITE,可自定义其他类型数据库。Support Muti-Type-Databases. > > 支持多数据库操作。Support Muti-Databases-Operate. > > 表单验证支持,HTTP请求数据可直接用来更新数据库。FormValidatee is supported, and Http Post data can be used for insert or update table record(s). > > 提供HttpRequest,HttpUpload,Soap,OAUTH2.0等模块。"HttpRequest,HttpUpload,Soap,OAUTH2.0" are supported. > > 提供CryptoJS,提供AES/DES/RC4/Rabbit/pbkdf2/ripemd160等算法。CryptoJS is supported. > > 内置Json解析和构建。Json2 is built-in.You can use it to parse or stringify Json data. > > 支持多种路由方式,包括404、URL、isapi_URLRewrite,完全自定义的路由配置。URLRoute is Supported(404 Error Page, URL Route,ISAPI_URIRewrite). > > 路由支持REST。REST is Supported. > > 支持类库缓存,编译缓存,HTML缓存,数据库Model缓存。Library Cache, Combined File Cache, HTML Cache and Model Cache. > > 资源统一管理,统一销毁,使您专心于业务逻辑处理。You can pay much more attention on you business.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值