();
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
+ }
+
+
+
-