ASP.NET MVC5 - AccountController - 修改

  • 去掉外部登陆
  • 使用MySql数据库
  • 用户Id使用长整型
  • 增加Email,并使用Email验证登陆

相对于微软的高大上,我更喜欢土鳖的做法!!!

SQL语句

CREATE TABLE `users` (
  `UserId` bigint(20) NOT NULL AUTO_INCREMENT,
  `UserName` varchar(50) NOT NULL,
  `Email` varchar(50) NOT NULL,
  `PwdHash` varchar(100) NOT NULL,
  `SecurityStamp` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`UserId`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

AccountController.cs代码

using System;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using Dapper;
using JFuil.Web.Models;
using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
using MySql.Data.MySqlClient;

namespace JFuil.Web.Controllers
{
    [Authorize]
    public class AccountController : Controller
    {
        MySqlStore _store;
        UserManager<MyUser> _userManager;

        public AccountController()
        {
            _store = new MySqlStore(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
            _userManager = new UserManager<MyUser>(_store);
        }

        //
        // GET: /Account/Login
        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        //
        // POST: /Account/Login
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var user = _store.FindUserByEmail(model.Email);

                if (user != null)
                {
                    if (Crypto.VerifyHashedPassword(user.PwdHash, model.Password))
                    {
                        await SignInAsync(user, model.RememberMe);
                        return RedirectToLocal(returnUrl);
                    }
                    else ModelState.AddModelError(string.Empty, "密码 不正确");
                }
                else ModelState.AddModelError(string.Empty, "电子邮箱 不存在");
            }

            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            return View(model);
        }

        // GET: /Account/Register
        [AllowAnonymous]
        public ActionResult Register()
        {
            return View();
        }

        //
        // POST: /Account/Register
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new MyUser()
                {
                    Email = model.Email,
                    UserName = model.UserName,
                    PwdHash = Crypto.HashPassword(model.Password)
                };

                if (_store.FindUserByEmail(model.Email) != null)
                {
                    ModelState.AddModelError(string.Empty, "电子邮箱 已被注册");
                }
                else
                {
                    var result = await _userManager.CreateAsync(user, model.Password);

                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: false);
                        return RedirectToAction("Index", "Home");
                    }
                    else AddErrors(result);
                }
            }

            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            return View(model);
        }

        //
        // GET: /Account/Manage
        public ActionResult Manage(ManageMessageId? message)
        {
            ViewBag.StatusMessage =
                message == ManageMessageId.ChangePasswordSuccess ? "你的密码已更改。"
                : message == ManageMessageId.SetPasswordSuccess ? "已设置你的密码。"
                : message == ManageMessageId.RemoveLoginSuccess ? "已删除外部登录名。"
                : message == ManageMessageId.Error ? "出现错误。"
                : "";
            ViewBag.HasLocalPassword = HasPassword();
            ViewBag.ReturnUrl = Url.Action("Manage");
            return View();
        }

        //
        // POST: /Account/Manage
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Manage(ManageUserViewModel model)
        {
            bool hasPassword = HasPassword();
            ViewBag.HasLocalPassword = hasPassword;
            ViewBag.ReturnUrl = Url.Action("Manage");
            if (hasPassword)
            {
                if (ModelState.IsValid)
                {
                    IdentityResult result = await _userManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);
                    if (result.Succeeded)
                    {
                        return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }
            else
            {
                // User does not have a password so remove any validation errors caused by a missing OldPassword field
                ModelState state = ModelState["OldPassword"];
                if (state != null)
                {
                    state.Errors.Clear();
                }

                if (ModelState.IsValid)
                {
                    IdentityResult result = await _userManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);
                    if (result.Succeeded)
                    {
                        return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess });
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }

            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            return View(model);
        }

        //
        // POST: /Account/LogOff
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LogOff()
        {
            AuthenticationManager.SignOut();
            return RedirectToAction("Index", "Home");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && _userManager != null)
            {
                _userManager.Dispose();
                _userManager = null;
            }
            base.Dispose(disposing);
        }

        #region 帮助程序

        private IAuthenticationManager AuthenticationManager
        {
            get
            {
                return HttpContext.GetOwinContext().Authentication;
            }
        }

        private async Task SignInAsync(MyUser user, bool isPersistent)
        {
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            var identity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
            AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
        }

        private void AddErrors(IdentityResult result)
        {
            foreach (var error in result.Errors)
            {
                ModelState.AddModelError("", error);
            }
        }

        private bool HasPassword()
        {
            var user = _userManager.FindById(User.Identity.GetUserId());
            if (user != null)
            {
                return user.PwdHash != null;
            }
            return false;
        }

        public enum ManageMessageId
        {
            ChangePasswordSuccess,
            SetPasswordSuccess,
            RemoveLoginSuccess,
            Error
        }

        private ActionResult RedirectToLocal(string returnUrl)
        {
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }

        #endregion

        class MyUser : IUser
        {
            public string Id
            {
                get { return UserId.ToString(); }
            }

            public long UserId { get; set; }

            public string UserName { get; set; }

            public string Email { get; set; }

            public string PwdHash { get; set; }
        }

        class MySqlStore : IUserStore<MyUser>, IUserPasswordStore<MyUser>
        {
            MySqlConnection _connection;

            public MySqlStore(string connStr)
            {
                _connection = new MySqlConnection(connStr);
            }

            public MyUser FindUserByEmail(string email)
            {
                return _connection.Query<MyUser>("SELECT * FROM USERS WHERE Email = @Email LIMIT 1", new { Email = email }).FirstOrDefault();
            }

            public Task CreateAsync(MyUser user)
            {
                var sql = "Insert into Users (Email, UserName, PwdHash) values (@Email, @UserName, @PwdHash)";
                _connection.Execute(sql, user);
                return Task.FromResult<object>(null);
            }

            public Task DeleteAsync(MyUser user)
            {
                _connection.Execute("Delete from Users where UserId =" + user.Id);
                return Task.FromResult<object>(null);
            }

            public Task<MyUser> FindByIdAsync(string userId)
            {
                if (string.IsNullOrEmpty(userId)) throw new ArgumentException("userId");
                long number;
                if (Int64.TryParse(userId, out number))
                {
                    var query = _connection.Query<MyUser>("Select * from Users where UserId=" + number.ToString());
                    return Task.FromResult<MyUser>(query.Any() ? query.First() : null);
                }
                return Task.FromResult<MyUser>(null);
            }

            public Task<MyUser> FindByNameAsync(string userName)
            {
                var query = _connection.Query<MyUser>("Select * from Users where UserName = @UserName", new { UserName = userName });
                return Task.FromResult<MyUser>(query.Any() ? query.First() : null);
            }

            public Task UpdateAsync(MyUser user)
            {
                var sql = "Update Users Set UserName = @UserName, PwdHash = @PwdHash WHERE UserId = @Id";
                _connection.Execute(sql, user);
                return Task.FromResult<object>(null);
            }

            public void Dispose()
            {
                if (_connection != null)
                {
                    _connection.Dispose();
                    _connection = null;
                }
            }

            #region IUserPasswordStore<MyUser>

            public Task<string> GetPasswordHashAsync(MyUser user)
            {
                return Task.FromResult<string>(_connection.Query<string>("Select PwdHash from Users where UserId =" + user.Id).FirstOrDefault());
            }

            public Task<bool> HasPasswordAsync(MyUser user)
            {
                return Task.FromResult<bool>(_connection.Query<string>("Select 1 from Users where UserId =" + user.Id + " limit 1").Any());
            }

            public Task SetPasswordHashAsync(MyUser user, string passwordHash)
            {
                user.PwdHash = passwordHash;
                return Task.FromResult<object>(null);
            }

            #endregion
        }
    }
}

ASP.NET MVC5 - AccountController - 修改,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。