diff --git a/Infrastructure/Infrastructure.csproj b/Infrastructure/Infrastructure.csproj index 174cf348..278fe132 100644 --- a/Infrastructure/Infrastructure.csproj +++ b/Infrastructure/Infrastructure.csproj @@ -84,6 +84,7 @@ + diff --git a/OpenAuth.WebApi/Areas/SSO/Models/StringExtensions.cs b/Infrastructure/StringExtensions.cs similarity index 95% rename from OpenAuth.WebApi/Areas/SSO/Models/StringExtensions.cs rename to Infrastructure/StringExtensions.cs index 7cdb49e6..019cefdd 100644 --- a/OpenAuth.WebApi/Areas/SSO/Models/StringExtensions.cs +++ b/Infrastructure/StringExtensions.cs @@ -1,7 +1,7 @@ using System.Security.Cryptography; using System.Text; -namespace OpenAuth.WebApi.Areas.SSO.Models +namespace Infrastructure { public static class StringExtensions { diff --git a/OpenAuth.App/LoginApp.cs b/OpenAuth.App/LoginApp.cs index b27fa436..02e598dd 100644 --- a/OpenAuth.App/LoginApp.cs +++ b/OpenAuth.App/LoginApp.cs @@ -1,9 +1,7 @@ -using System; -using System.Linq; +using System.Linq; using System.Web; using Infrastructure; using OpenAuth.App.ViewModel; -using System.Web.Security; using OpenAuth.App.SSO; using OpenAuth.Domain.Service; diff --git a/OpenAuth.App/OpenAuth.App.csproj b/OpenAuth.App/OpenAuth.App.csproj index 5d0b7ab9..8db860fe 100644 --- a/OpenAuth.App/OpenAuth.App.csproj +++ b/OpenAuth.App/OpenAuth.App.csproj @@ -79,11 +79,17 @@ + + + + + + diff --git a/OpenAuth.WebApi/Areas/SSO/Models/AppInfo.cs b/OpenAuth.App/SSO/AppInfo.cs similarity index 95% rename from OpenAuth.WebApi/Areas/SSO/Models/AppInfo.cs rename to OpenAuth.App/SSO/AppInfo.cs index 2bd15d14..9c828226 100644 --- a/OpenAuth.WebApi/Areas/SSO/Models/AppInfo.cs +++ b/OpenAuth.App/SSO/AppInfo.cs @@ -11,7 +11,7 @@ using System; -namespace OpenAuth.WebApi.Areas.SSO.Models +namespace OpenAuth.App.SSO { /// /// 应用程序信息 diff --git a/OpenAuth.App/SSO/AppInfoService.cs b/OpenAuth.App/SSO/AppInfoService.cs new file mode 100644 index 00000000..794c62b8 --- /dev/null +++ b/OpenAuth.App/SSO/AppInfoService.cs @@ -0,0 +1,38 @@ +using System; +using System.Linq; + +namespace OpenAuth.App.SSO +{ + public class AppInfoService : ServiceContext + { + public AppInfo Get(string appKey) + { + //可以从数据库读取 + return _applist.SingleOrDefault(u => u.AppKey == appKey); + } + + private AppInfo[] _applist = new[] + { + new AppInfo + { + AppKey = "openauth", + Icon = "/Areas/SSO/Content/images/logo.png", + IsEnable = true, + Remark = "基于DDDLite的权限管理系统", + ReturnUrl = "http://localhost:56813", + Title = "OpenAuth.Net", + CreateTime = DateTime.Now, + }, + new AppInfo + { + AppKey = "openauthtest", + Icon = "/Areas/SSO/Content/images/logo.png", + IsEnable = true, + Remark = "这只是个模拟的测试站点", + ReturnUrl = "http://localhost:53050", + Title = "OpenAuth.Net测试站点", + CreateTime = DateTime.Now, + } + }; + } +} \ No newline at end of file diff --git a/OpenAuth.App/SSO/AuthUtil.cs b/OpenAuth.App/SSO/AuthUtil.cs index 6fea12bd..fcb730ab 100644 --- a/OpenAuth.App/SSO/AuthUtil.cs +++ b/OpenAuth.App/SSO/AuthUtil.cs @@ -18,6 +18,15 @@ using OpenAuth.App.ViewModel; namespace OpenAuth.App.SSO { + /// + /// վ¼֤ + /// ¼ʱ + /// + /// var result = AuthUtil.Login(AppKey, username, password); + /// if (result.Success) + /// return Redirect("/home/index?Token=" + result.Token); + /// + /// public class AuthUtil { static HttpHelper _helper = new HttpHelper(ConfigurationManager.AppSettings["SSOPassport"]); @@ -25,23 +34,23 @@ namespace OpenAuth.App.SSO private static string GetToken() { string token = HttpContext.Current.Request.QueryString["Token"]; - if (!string.IsNullOrEmpty(token)) return token; + if (!String.IsNullOrEmpty(token)) return token; var cookie = HttpContext.Current.Request.Cookies["Token"]; - return cookie == null ? string.Empty : cookie.Value; + return cookie == null ? String.Empty : cookie.Value; } public static bool CheckLogin(string token, string remark = "") { - if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(GetToken())) + if (String.IsNullOrEmpty(token) || String.IsNullOrEmpty(GetToken())) return false; - var requestUri = string.Format("/SSO/Check/GetStatus?token={0}&requestid={1}", token, remark); + var requestUri = String.Format("/SSO/Check/GetStatus?token={0}&requestid={1}", token, remark); try { var value = _helper.Get(null, requestUri); - return bool.Parse(value); + return Boolean.Parse(value); } catch (Exception ex) { @@ -49,15 +58,26 @@ namespace OpenAuth.App.SSO } } + /// + /// û¼״̬ + /// ͨURLеTokenCookieеToken + /// + /// עϢ public static bool CheckLogin(string remark="") { return CheckLogin(GetToken(), remark); } + /// + /// ȡǰ¼ûϢ + /// ͨURLеTokenCookieеToken + /// + /// The remark. + /// LoginUserVM. public static LoginUserVM GetCurrentUser(string remark = "") { - var requestUri = string.Format("/SSO/Check/GetUser?token={0}&requestid={1}", GetToken(), remark); + var requestUri = String.Format("/SSO/Check/GetUser?token={0}&requestid={1}", GetToken(), remark); try { @@ -79,7 +99,7 @@ namespace OpenAuth.App.SSO /// System.String. public static LoginResult Login(string appKey, string username, string pwd) { - var requestUri = "/SSO/Login/Check"; + var requestUri = "/SSO/Check/Login"; try { @@ -106,9 +126,9 @@ namespace OpenAuth.App.SSO public static bool Logout() { var token = GetToken(); - if (string.IsNullOrEmpty(token)) return true; + if (String.IsNullOrEmpty(token)) return true; - var requestUri = string.Format("/SSO/Login/Logout?token={0}&requestid={1}", token, ""); + var requestUri = String.Format("/SSO/Login/Logout?token={0}&requestid={1}", token, ""); try { diff --git a/OpenAuth.WebApi/Areas/SSO/Models/PassportLoginRequest.cs b/OpenAuth.App/SSO/PassportLoginRequest.cs similarity index 51% rename from OpenAuth.WebApi/Areas/SSO/Models/PassportLoginRequest.cs rename to OpenAuth.App/SSO/PassportLoginRequest.cs index 960b5e15..c5c20d0d 100644 --- a/OpenAuth.WebApi/Areas/SSO/Models/PassportLoginRequest.cs +++ b/OpenAuth.App/SSO/PassportLoginRequest.cs @@ -1,26 +1,19 @@ -using System.ComponentModel; -using System.ComponentModel.DataAnnotations; - -namespace OpenAuth.WebApi.Areas.SSO.Models +namespace OpenAuth.App.SSO { public class PassportLoginRequest { - [DisplayName("邮箱地址")] public string UserName { get; set; } - [Required] - [DisplayName("登录密码")] public string Password { get; set; } - [Display(Name = "应用标识")] public string AppKey { get; set; } public void Trim() { UserName = UserName.Trim(); Password = Password.Trim(); - AppKey = AppKey.Trim(); + if(!string.IsNullOrEmpty(AppKey)) AppKey = AppKey.Trim(); } } } \ No newline at end of file diff --git a/OpenAuth.App/SSO/SSOAuthUtil.cs b/OpenAuth.App/SSO/SSOAuthUtil.cs new file mode 100644 index 00000000..7d48d50c --- /dev/null +++ b/OpenAuth.App/SSO/SSOAuthUtil.cs @@ -0,0 +1,62 @@ +using System; +using System.Web; +using System.Web.Mvc; +using Infrastructure; + +namespace OpenAuth.App.SSO +{ + public class SSOAuthUtil + { + public static LoginResult Parse(PassportLoginRequest model) + { + model.Trim(); + + var result = new LoginResult(); + + try + { + //ȡӦϢ + var appInfo = new AppInfoService().Get(model.AppKey); + if (appInfo == null) + { + throw new Exception("Ӧò"); + } + //ȡûϢ + var usermanager = (UserManagerApp) DependencyResolver.Current.GetService(typeof (UserManagerApp)); + var userInfo = usermanager.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 = HttpContext.Current.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 = ex.Message; + } + + return result; + } + } +} \ No newline at end of file diff --git a/OpenAuth.WebApi/Areas/SSO/Models/ServiceContext.cs b/OpenAuth.App/SSO/ServiceContext.cs similarity index 97% rename from OpenAuth.WebApi/Areas/SSO/Models/ServiceContext.cs rename to OpenAuth.App/SSO/ServiceContext.cs index 195a9a41..5861ba9c 100644 --- a/OpenAuth.WebApi/Areas/SSO/Models/ServiceContext.cs +++ b/OpenAuth.App/SSO/ServiceContext.cs @@ -2,7 +2,7 @@ using System.Globalization; using Infrastructure.Cache; -namespace OpenAuth.WebApi.Areas.SSO.Models +namespace OpenAuth.App.SSO { public abstract class ServiceContext : IDisposable { diff --git a/OpenAuth.App/SSO/UserAuthSession.cs b/OpenAuth.App/SSO/UserAuthSession.cs index d3687eb3..fb2a768a 100644 --- a/OpenAuth.App/SSO/UserAuthSession.cs +++ b/OpenAuth.App/SSO/UserAuthSession.cs @@ -1,6 +1,6 @@ using System; -namespace OpenAuth.WebApi.Areas.SSO.Models +namespace OpenAuth.App.SSO { [Serializable] public class UserAuthSession diff --git a/OpenAuth.WebApi/Areas/SSO/Models/Services/UserAuthSessionService.cs b/OpenAuth.App/SSO/UserAuthSessionService.cs similarity index 64% rename from OpenAuth.WebApi/Areas/SSO/Models/Services/UserAuthSessionService.cs rename to OpenAuth.App/SSO/UserAuthSessionService.cs index 6f26100d..892bedd8 100644 --- a/OpenAuth.WebApi/Areas/SSO/Models/Services/UserAuthSessionService.cs +++ b/OpenAuth.App/SSO/UserAuthSessionService.cs @@ -1,8 +1,24 @@ -using System; +// *********************************************************************** +// Assembly : OpenAuth.WebApi +// Author : yubaolee +// Created : 07-11-2016 +// +// Last Modified By : yubaolee +// Last Modified On : 07-11-2016 +// Contact : +// File: UserAuthSessionService.cs +// *********************************************************************** + +using System; using Infrastructure.Cache; -namespace OpenAuth.WebApi.Areas.SSO.Models.Services +namespace OpenAuth.App.SSO { + /// + /// 用户登录状态存储服务 + /// 测试环境用的是基于http application的SessionContext + /// 正式环境可以使用基于memcached的EnyimMemcachedContext + /// public class UserAuthSessionService : ServiceContext { public UserAuthSessionService() diff --git a/OpenAuth.Mvc/Controllers/LoginController.cs b/OpenAuth.Mvc/Controllers/LoginController.cs index 9b49c789..de1db712 100644 --- a/OpenAuth.Mvc/Controllers/LoginController.cs +++ b/OpenAuth.Mvc/Controllers/LoginController.cs @@ -7,7 +7,7 @@ namespace OpenAuth.Mvc.Controllers { public class LoginController : Controller { - private const string AppKey = "670b14728ad9902aecba32e22fa4f6bd"; + private const string AppKey = "openauth"; // GET: Login public ActionResult Index() diff --git a/OpenAuth.Mvc/Views/Login/Index.cshtml b/OpenAuth.Mvc/Views/Login/Index.cshtml index 50c11fe8..fc60d693 100644 --- a/OpenAuth.Mvc/Views/Login/Index.cshtml +++ b/OpenAuth.Mvc/Views/Login/Index.cshtml @@ -237,7 +237,8 @@ 可以用admin(密码:admin) /test(密码:test) 查看不同账号登陆情况 - Copyright © 2015 基于经典DDD的权限管理 - 点击以开发者账号登录 + Copyright © 2015 基于经典DDD的权限管理 - 点击以开发者账号登录 + 或者使用OpenAuth.net第三方登陆功能
+ /// var result = AuthUtil.Login(AppKey, username, password); + /// if (result.Success) + /// return Redirect("/home/index?Token=" + result.Token); + ///