diff --git a/NX.ERP.U8/NX.ERP.U8.csproj b/NX.ERP.U8/NX.ERP.U8.csproj index fd4551a..d1e74a6 100644 --- a/NX.ERP.U8/NX.ERP.U8.csproj +++ b/NX.ERP.U8/NX.ERP.U8.csproj @@ -62,8 +62,10 @@ + + @@ -86,6 +88,7 @@ + E:\U8SOFT\UFMOM\U8APIFramework\UFIDA.U8.MomServiceCommon.dll diff --git a/NX.ERP.U8/U8LoginContext.cs b/NX.ERP.U8/U8LoginContext.cs index 9a91d14..8d67f69 100644 --- a/NX.ERP.U8/U8LoginContext.cs +++ b/NX.ERP.U8/U8LoginContext.cs @@ -10,10 +10,12 @@ namespace NX.ERP.U8 { public class U8LoginContext : IDisposable { + #region 单例 private static readonly object locker = new object(); private static U8LoginContext _u8LoginContext; - private string EaiPort = System.Configuration.ConfigurationManager.AppSettings["EaiPort"]; - private string EaiPath = System.Configuration.ConfigurationManager.AppSettings["EaiPath"]; + /// + /// 默认实例 + /// public static U8LoginContext Instance { get @@ -25,6 +27,7 @@ namespace NX.ERP.U8 if (_u8LoginContext == null) { _u8LoginContext = new U8LoginContext(); + } } @@ -35,6 +38,10 @@ namespace NX.ERP.U8 private UFSoft.U8.Framework.Login.UI.clsLogin uiLogin; private clsLogin _clslogin; + #endregion + + private string EaiPort = System.Configuration.ConfigurationManager.AppSettings["EaiPort"]; + private string EaiPath = System.Configuration.ConfigurationManager.AppSettings["EaiPath"]; public string cAcc_id { @@ -44,9 +51,29 @@ namespace NX.ERP.U8 } } + /// + /// + /// public U8LoginContext() { } + + /// + /// + /// + /// + /// + public U8LoginContext(string Server, string Accid) + { + this._server = Server; + this._accid = Accid; + } + + public U8LoginContext(string userid, string password, string server, string accid) : this(server, accid) + { + this._password = password; + this._userId = userid; + } private static readonly object locker1 = new object(); public clsLogin ClsLogin @@ -256,35 +283,18 @@ namespace NX.ERP.U8 string.IsNullOrEmpty(this._password) || string.IsNullOrEmpty(this._server) || string.IsNullOrEmpty(this._accid); - - } } - public void Login() + + public void Login(string userId, string password) { - - - - if (NeedUiLogin) + if (!UiLogin.login("DP", userId, password, this._server, DateTime.Today.ToString("yyyy-MM-dd"), this._accid, Serial)) { - if (!UiLogin.login("DP")) - { - - throw new Exception(UiLogin.ErrDescript); - - } + throw new Exception(UiLogin.ErrDescript); } - else - { - if (!UiLogin.login("DP", this._userId, this._password, this._server, DateTime.Today.ToString("yyyy-MM-dd"), this._accid, Serial)) - { - throw new Exception(UiLogin.ErrDescript); - } - } - if (UiLogin.SubLogin("ST") == null) { @@ -299,6 +309,62 @@ namespace NX.ERP.U8 } + /// + /// 登录ID 唯一凭据 + /// + public string loginId + { + get + { + return this.UserToken == null ? "" : this.UserToken.UserTokenData.SignedToken.Id; + } + } + + + public ufsoft UserToken + { + get + { + if (this._clslogin == null) + { + return null; + } + if (this._clslogin.userToken=="") + { + return null; + } + return XmlHelper.XmlDeserialize(this._clslogin.userToken, Encoding.UTF8); + } + } + public void Login() + { + if (NeedUiLogin) + { + if (!UiLogin.login("DP")) + { + throw new Exception(UiLogin.ErrDescript); + } + } + else + { + if (!UiLogin.login("DP", this._userId, this._password, this._server, DateTime.Today.ToString("yyyy-MM-dd"), this._accid, Serial)) + { + throw new Exception(UiLogin.ErrDescript); + } + } + + if (UiLogin.SubLogin("ST") == null) + { + throw new Exception(UiLogin.ErrDescript); + } + this._clslogin = new clsLoginClass(); + if (!_clslogin.ConstructLogin(UiLogin.userToken)) + { + throw new Exception(_clslogin.ShareString); + } + _clslogin.set_TaskId(UiLogin.GetTaskID("ST")); + } + diff --git a/NX.ERP.U8/XmlHelper.cs b/NX.ERP.U8/XmlHelper.cs new file mode 100644 index 0000000..48105a7 --- /dev/null +++ b/NX.ERP.U8/XmlHelper.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; +using System.Xml; +using System.Xml.Serialization; + +namespace NX.ERP.U8 +{ + /// + /// XML转化对象 + /// + public static class XmlHelper + { + private static void XmlSerializeInternal(Stream stream, object o, Encoding encoding) + { + XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); + ns.Add("", ""); + XmlSerializer serializer = new XmlSerializer(o.GetType()); + XmlWriterSettings settings = new XmlWriterSettings(); + settings.Indent = true; + settings.NewLineChars = "\r\n"; + settings.Encoding = encoding; + settings.IndentChars = " "; + //settings.NamespaceHandling = NamespaceHandling.OmitDuplicates; + using (XmlWriter writer = XmlWriter.Create(stream, settings)) + { + serializer.Serialize(writer, o, ns); + writer.Close(); + } + } + + /// + /// 将一个对象序列化为XML字符串 + /// + /// 要序列化的对象 + /// 编码方式 + /// 序列化产生的XML字符串 + public static string XmlSerialize(object o, Encoding encoding) + { + using (MemoryStream stream = new MemoryStream()) + { + XmlSerializeInternal(stream, o, encoding); + + stream.Position = 0; + using (StreamReader reader = new StreamReader(stream, encoding)) + { + return reader.ReadToEnd(); + } + } + } + + /// + /// + /// + /// + /// + /// + /// + public static T XmlDeserialize(string s, Encoding encoding) + { + if (string.IsNullOrEmpty(s)) + throw new ArgumentNullException("s"); + if (encoding == null) + throw new ArgumentNullException("encoding"); + + XmlSerializer mySerializer = new XmlSerializer(typeof(T)); + s = s.Replace(" ", " "); + using (MemoryStream ms = new MemoryStream(encoding.GetBytes(s))) + { + using (StreamReader sr = new StreamReader(ms, encoding)) + { + return (T)mySerializer.Deserialize(sr); + } + } + } + + } + +} diff --git a/NX.ERP.U8/ufsoft.cs b/NX.ERP.U8/ufsoft.cs new file mode 100644 index 0000000..db8be6b --- /dev/null +++ b/NX.ERP.U8/ufsoft.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NX.ERP.U8 +{ + + public class ufsoft + { + [System.Xml.Serialization.XmlElement(Type = typeof(UserTokenData), ElementName = "data")] + public UserTokenData UserTokenData { get; set; } + [System.Xml.Serialization.XmlElement(Type = typeof(AppServer), ElementName = "AppServerName")] + public AppServer AppServerName { get; set; } + } + + + public class AppServer + { + [System.Xml.Serialization.XmlAttribute] + public string Name { get; set; } + + } + + public class UserTokenData + { + [System.Xml.Serialization.XmlElement(Type = typeof(SignedToken), ElementName = "SignedToken")] + public SignedToken SignedToken { get; set; } + } + + + public class SignedToken + { + [System.Xml.Serialization.XmlAttribute(AttributeName="id")] + public string Id { get; set; } + [System.Xml.Serialization.XmlAttribute(AttributeName = "value")] + public string Value { get; set; } + + } + + + +} diff --git a/NX.ERP.WebAPi/App_Start/FilterConfig.cs b/NX.ERP.WebAPi/App_Start/FilterConfig.cs index 004b8e7..8c5edbf 100644 --- a/NX.ERP.WebAPi/App_Start/FilterConfig.cs +++ b/NX.ERP.WebAPi/App_Start/FilterConfig.cs @@ -8,6 +8,7 @@ namespace NX.ERP.WebAPi public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); + } } } diff --git a/NX.ERP.WebAPi/Controllers/System/SystemController.cs b/NX.ERP.WebAPi/Controllers/System/SystemController.cs new file mode 100644 index 0000000..e9165ce --- /dev/null +++ b/NX.ERP.WebAPi/Controllers/System/SystemController.cs @@ -0,0 +1,30 @@ +using NX.ERP.WebAPi.models; +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Web; +using System.Web.Http; +namespace NX.ERP.WebAPi.Controllers.System +{ + + public class SystemController:ApiController + { + [HttpPost] + [Route("api/System/Login")] + public string Login(LoginEntity loginEntity) + { + var AccID = ConfigurationManager.AppSettings["AccId"]; + //NX.ERP.U8.U8LoginContext.Instance.PassWord = System.Configuration.ConfigurationManager.AppSettings["Password"]; + + //NX.ERP.U8.U8LoginContext.Instance.UserId = System.Configuration.ConfigurationManager.AppSettings["UserId"]; + + var Server =ConfigurationManager.AppSettings["Server"]; + var _login = new NX.ERP.U8.U8LoginContext(Server, AccID); + _login.Login(loginEntity.UserId, loginEntity.Password); + return _login.loginId; + + + } + } +} \ No newline at end of file diff --git a/NX.ERP.WebAPi/NX.ERP.WebAPi.csproj b/NX.ERP.WebAPi/NX.ERP.WebAPi.csproj index 44b649e..2d03a67 100644 --- a/NX.ERP.WebAPi/NX.ERP.WebAPi.csproj +++ b/NX.ERP.WebAPi/NX.ERP.WebAPi.csproj @@ -57,6 +57,12 @@ True + + ..\packages\Microsoft.Owin.4.0.1\lib\net45\Microsoft.Owin.dll + + + ..\packages\Owin.1.0\lib\net40\Owin.dll + ..\packages\Swashbuckle.Core.5.6.0\lib\net40\Swashbuckle.Core.dll @@ -168,13 +174,16 @@ + Global.asax + + diff --git a/NX.ERP.WebAPi/Startup.cs b/NX.ERP.WebAPi/Startup.cs new file mode 100644 index 0000000..4bb800d --- /dev/null +++ b/NX.ERP.WebAPi/Startup.cs @@ -0,0 +1,18 @@ +using Microsoft.Owin; +using Owin; +using System; +using System.Threading.Tasks; + +[assembly: OwinStartup(typeof(NX.ERP.WebAPi.Startup))] + +namespace NX.ERP.WebAPi +{ + public class Startup + { + public void Configuration(IAppBuilder app) + { + + + } + } +} diff --git a/NX.ERP.WebAPi/Web.config b/NX.ERP.WebAPi/Web.config index 3c461db..9ec58cd 100644 --- a/NX.ERP.WebAPi/Web.config +++ b/NX.ERP.WebAPi/Web.config @@ -5,11 +5,11 @@ --> - - - - - + + + + + diff --git a/NX.ERP.WebAPi/models/LoginEntity.cs b/NX.ERP.WebAPi/models/LoginEntity.cs new file mode 100644 index 0000000..b3325b3 --- /dev/null +++ b/NX.ERP.WebAPi/models/LoginEntity.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace NX.ERP.WebAPi.models +{ + public class LoginEntity + { + private string userId; + private string password; + + public string UserId { get => userId; set => userId = value; } + public string Password { get => password; set => password = value; } + } +} \ No newline at end of file diff --git a/NX.ERP.WebAPi/packages.config b/NX.ERP.WebAPi/packages.config index 3e4d02b..727b929 100644 --- a/NX.ERP.WebAPi/packages.config +++ b/NX.ERP.WebAPi/packages.config @@ -21,9 +21,11 @@ + +