From 994ab81ebb495c04df6c6017051e80aedec2c892 Mon Sep 17 00:00:00 2001 From: yubaolee Date: Mon, 11 Jul 2016 18:21:26 +0800 Subject: [PATCH] check some bugs --- OpenAuth.App/SSO/AuthUtil.cs | 14 +--- OpenAuth.App/UserManagerApp.cs | 5 ++ OpenAuth.Mvc/Controllers/LoginController.cs | 19 ++--- .../Areas/SSO/Controllers/LoginController.cs | 79 ++++++++++--------- OpenAuth.WebApi/Areas/SSO/Models/AppUser.cs | 41 ---------- .../SSO/Models/Services/AppUserService.cs | 16 ---- OpenAuth.WebApi/OpenAuth.WebApi.csproj | 2 - .../Controllers/HomeController.cs | 3 + .../Controllers/LoginController.cs | 14 ++-- OpenAuth.WebTest/Views/Login/Index.cshtml | 57 +++++++------ 10 files changed, 100 insertions(+), 150 deletions(-) delete mode 100644 OpenAuth.WebApi/Areas/SSO/Models/AppUser.cs delete mode 100644 OpenAuth.WebApi/Areas/SSO/Models/Services/AppUserService.cs diff --git a/OpenAuth.App/SSO/AuthUtil.cs b/OpenAuth.App/SSO/AuthUtil.cs index fad765cf..6fea12bd 100644 --- a/OpenAuth.App/SSO/AuthUtil.cs +++ b/OpenAuth.App/SSO/AuthUtil.cs @@ -77,7 +77,7 @@ namespace OpenAuth.App.SSO /// û /// /// System.String. - public static string Login(string appKey, string username, string pwd) + public static LoginResult Login(string appKey, string username, string pwd) { var requestUri = "/SSO/Login/Check"; @@ -91,18 +91,12 @@ namespace OpenAuth.App.SSO }, requestUri); var result = JsonHelper.Instance.Deserialize(value); - if (result.Success) - { - return result.Token; - } - else - { - return string.Empty; - } + return result; + } catch (Exception ex) { - return string.Empty; + return null; } } diff --git a/OpenAuth.App/UserManagerApp.cs b/OpenAuth.App/UserManagerApp.cs index fe9b1b44..201ebe26 100644 --- a/OpenAuth.App/UserManagerApp.cs +++ b/OpenAuth.App/UserManagerApp.cs @@ -22,6 +22,11 @@ namespace OpenAuth.App _relevanceRepository = relevanceRepository; } + public User Get(string account) + { + return _repository.FindSingle(u => u.Account == account); + } + public int GetUserCntInOrg(int orgId) { if (orgId == 0) diff --git a/OpenAuth.Mvc/Controllers/LoginController.cs b/OpenAuth.Mvc/Controllers/LoginController.cs index 26a0fc1e..9b49c789 100644 --- a/OpenAuth.Mvc/Controllers/LoginController.cs +++ b/OpenAuth.Mvc/Controllers/LoginController.cs @@ -1,6 +1,5 @@ using System; using System.Web.Mvc; -using OpenAuth.App; using OpenAuth.App.SSO; using OpenAuth.Mvc.Models; @@ -8,12 +7,8 @@ namespace OpenAuth.Mvc.Controllers { public class LoginController : Controller { - private LoginApp _app; + private const string AppKey = "670b14728ad9902aecba32e22fa4f6bd"; - public LoginController() - { - _app = AutofacExt.GetFromFac(); - } // GET: Login public ActionResult Index() { @@ -25,9 +20,9 @@ namespace OpenAuth.Mvc.Controllers { try { - var token = AuthUtil.Login("670b14728ad9902aecba32e22fa4f6bd", username, password); - if (!string.IsNullOrEmpty(token)) - return Redirect("/home/index?Token=" + token); + var result = AuthUtil.Login(AppKey, username, password); + if (result.Success) + return Redirect("/home/index?Token=" + result.Token); else { var response = new BjuiResponse @@ -57,9 +52,9 @@ namespace OpenAuth.Mvc.Controllers { try { - var token = AuthUtil.Login("670b14728ad9902aecba32e22fa4f6bd", "System","123456"); - if (!string.IsNullOrEmpty(token)) - return Redirect("/home/index?Token=" + token); + var result = AuthUtil.Login(AppKey, "System","123456"); + if (result.Success) + return Redirect("/home/index?Token=" + result.Token); else { return RedirectToAction("Index", "Login"); diff --git a/OpenAuth.WebApi/Areas/SSO/Controllers/LoginController.cs b/OpenAuth.WebApi/Areas/SSO/Controllers/LoginController.cs index 301ddaf2..16dd087b 100644 --- a/OpenAuth.WebApi/Areas/SSO/Controllers/LoginController.cs +++ b/OpenAuth.WebApi/Areas/SSO/Controllers/LoginController.cs @@ -1,6 +1,7 @@ using System; using System.Web.Mvc; using Newtonsoft.Json; +using OpenAuth.App; using OpenAuth.App.SSO; using OpenAuth.WebApi.Areas.SSO.Models; using OpenAuth.WebApi.Areas.SSO.Models.Services; @@ -15,8 +16,7 @@ namespace OpenAuth.WebApi.Areas.SSO.Controllers public class LoginController : Controller { private readonly AppInfoService _appInfoService = new AppInfoService(); - private readonly AppUserService _appUserService = new AppUserService(); - + private UserManagerApp _useraApp = AutofacExt.GetFromFac(); private const string AppInfo = "AppInfo"; //默认登录界面 @@ -77,45 +77,50 @@ namespace OpenAuth.WebApi.Areas.SSO.Controllers var result = new LoginResult(); - //获取应用信息 - var appInfo = _appInfoService.Get(model.AppKey); - if (appInfo == null) + try + { + //获取应用信息 + var appInfo = _appInfoService.Get(model.AppKey); + if (appInfo == null) + { + throw new Exception("应用不存在"); + } + TempData[AppInfo] = appInfo; + + //获取用户信息 + var userInfo = _useraApp.Get(model.UserName); + if (userInfo == null) + { + throw new Exception("用户不存在"); + } + if (userInfo.Password != model.Password) + { + throw new Exception("密码错误"); + } + + var currentSession = new UserAuthSession + { + UserName = model.UserName, + Token = Guid.NewGuid().ToString().ToMd5(), + InvalidTime = DateTime.Now.AddMinutes(10), + AppKey = model.AppKey, + CreateTime = DateTime.Now, + IpAddress = Request.UserHostAddress + }; + + //创建Session + new UserAuthSessionService().Create(currentSession); + + result.Success = true; + result.ReturnUrl = appInfo.ReturnUrl; + result.Token = currentSession.Token; + } + catch (Exception ex) { result.Success = false; - result.ErrorMsg = "应用不存在"; - } - TempData[AppInfo] = appInfo; - - //获取用户信息 - var userInfo = _appUserService.Get(model.UserName); - if (userInfo == null) - { - result.Success = false; - result.ErrorMsg = "用户不存在"; + result.ErrorMsg = ex.Message; } - //if (userInfo.UserPwd != model.Password.ToMd5()) - //{ - // //密码不正确 - // return View(model); - //} - - var currentSession = new UserAuthSession - { - UserName = model.UserName, - Token = Guid.NewGuid().ToString().ToMd5(), - InvalidTime = DateTime.Now.AddMinutes(10), - AppKey = model.AppKey, - CreateTime = DateTime.Now, - IpAddress = Request.UserHostAddress - }; - - //创建Session - new UserAuthSessionService().Create(currentSession); - - result.Success = true; - result.ReturnUrl = appInfo.ReturnUrl; - result.Token = currentSession.Token; return result; } } diff --git a/OpenAuth.WebApi/Areas/SSO/Models/AppUser.cs b/OpenAuth.WebApi/Areas/SSO/Models/AppUser.cs deleted file mode 100644 index 8f2143a2..00000000 --- a/OpenAuth.WebApi/Areas/SSO/Models/AppUser.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; - -namespace OpenAuth.WebApi.Areas.SSO.Models -{ - public class AppUser - { - /// - /// 登录账号 - /// - [Key] - [MaxLength(50)] - public string UserName { get; set; } - - /// - /// 登录密码 - /// - [Required] - [MaxLength(32)] - public string UserPwd { get; set; } - - /// - /// 昵称 - /// - [Required] - [MaxLength(50)] - public string Nick { get; set; } - - /// - /// 是否启用 - /// - [Required] - public bool IsEnable { get; set; } - - /// - /// 创建时间 - /// - [Required] - public DateTime CreateTime { get; set; } - } -} \ No newline at end of file diff --git a/OpenAuth.WebApi/Areas/SSO/Models/Services/AppUserService.cs b/OpenAuth.WebApi/Areas/SSO/Models/Services/AppUserService.cs deleted file mode 100644 index 5c86c74b..00000000 --- a/OpenAuth.WebApi/Areas/SSO/Models/Services/AppUserService.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace OpenAuth.WebApi.Areas.SSO.Models.Services -{ - public class AppUserService : ServiceContext - { - public AppUser Get(string username = "") - { - //模拟用户 - return new AppUser - { - Nick = "超级管理员", - UserName = username, - UserPwd = "xxxxxxxxx" - }; - } - } -} \ No newline at end of file diff --git a/OpenAuth.WebApi/OpenAuth.WebApi.csproj b/OpenAuth.WebApi/OpenAuth.WebApi.csproj index d12ae22a..683fba51 100644 --- a/OpenAuth.WebApi/OpenAuth.WebApi.csproj +++ b/OpenAuth.WebApi/OpenAuth.WebApi.csproj @@ -152,11 +152,9 @@ - - diff --git a/OpenAuth.WebTest/Controllers/HomeController.cs b/OpenAuth.WebTest/Controllers/HomeController.cs index 59ac1f79..b4e491e0 100644 --- a/OpenAuth.WebTest/Controllers/HomeController.cs +++ b/OpenAuth.WebTest/Controllers/HomeController.cs @@ -16,6 +16,9 @@ namespace OpenAuth.WebTest.Controllers return View(); } + /// + /// 跳转到后台管理页面 + /// public ActionResult Admin() { return Redirect(ConfigurationManager.AppSettings["OpenAuthURL"] + "?token=" + Request.Cookies["Token"].Value); diff --git a/OpenAuth.WebTest/Controllers/LoginController.cs b/OpenAuth.WebTest/Controllers/LoginController.cs index ce463487..da2915ff 100644 --- a/OpenAuth.WebTest/Controllers/LoginController.cs +++ b/OpenAuth.WebTest/Controllers/LoginController.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; -using System.Web.Mvc; +using System.Web.Mvc; using OpenAuth.App.SSO; namespace OpenAuth.WebTest.Controllers @@ -18,12 +14,12 @@ namespace OpenAuth.WebTest.Controllers [HttpPost] public ActionResult Index(string username, string password) { - var token = AuthUtil.Login("670b14728ad9902aecba32e22fa4f6bd", username, password); - if (!string.IsNullOrEmpty(token)) - return Redirect("/home/index?Token=" + token); + var result = AuthUtil.Login("670b14728ad9902aecba32e22fa4f6bd", username, password); + if (result.Success) + return Redirect("/home/index?Token=" + result.Token); else { - return View(); + return View(result); } } diff --git a/OpenAuth.WebTest/Views/Login/Index.cshtml b/OpenAuth.WebTest/Views/Login/Index.cshtml index 58f7c107..e42e6fd0 100644 --- a/OpenAuth.WebTest/Views/Login/Index.cshtml +++ b/OpenAuth.WebTest/Views/Login/Index.cshtml @@ -1,28 +1,39 @@ -@{ +@model OpenAuth.App.SSO.LoginResult + +@{ ViewBag.Title = "title"; }

OpenAuth.net测试站点登陆

+
+ @if (Model != null && !Model.Success) + { + @Model.ErrorMsg + } +
+
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+ + +
+
+
+
+ -
-
- -
- -
-
-
- -
- -
-
-
-
- - -
-
-