Ejemplo n.º 1
0
        public void AunthenticateUserTest()
        {
            string         authToken      = @"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0b3B0YWwuY29tIiwiZXhwIjoxNDI2NDIwODAwL
                           CJodHRwOi8vdG9wdGFsLmNvbS9qd3RfY2xhaW1zL2lzX2FkbWluIjp0cnVlLCJjb21wYW55IjoiVG9wdGFsIiwiYXdlc29tZSI6dHJ1ZX0
                            .yRQYnWzskCZUxPwaQupWkiUzKELZ49eM7oWxAQK_ZXw";
            UserAuthResult userAuthResult = new UserAuthResult()
            {
                AuthToken = authToken,
                UserId    = 1
            };
            var mediator = new Mock <IMediator>();
            AuthenticateUserCommand command = new AuthenticateUserCommand
            {
                Name     = "Admin",
                Password = "******"
            };

            mediator.Setup(e => e.Send(command, new System.Threading.CancellationToken())).Returns(Task.FromResult(userAuthResult));
            UserController controller = new UserController(mediator.Object);
            var            result     = controller.Authenticate(command);
            var            response   = result.Result as OkObjectResult;

            Assert.AreEqual(1, ((UserAuthResult)response.Value).UserId);
            Assert.AreEqual(authToken, ((UserAuthResult)response.Value).AuthToken);
        }
Ejemplo n.º 2
0
 public void Logoff(string token)
 {
     if (UserAuthResult <CurrentUserInfo> .Current(token) != null)
     {
         UserAuthResult <CurrentUserInfo> .Current(token).Remove();
     }
 }
Ejemplo n.º 3
0
        public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
        {
            string token = string.Empty;

            if (request.Headers.MessageVersion.Envelope == EnvelopeVersion.None)
            {
                token = System.ServiceModel.Web.WebOperationContext.Current.IncomingRequest.Headers["Token"];
            }
            else
            {
                token = GetHeaderValue("Token");
            }

            AuthManager.SaveToken(token);
            CurrentUserInfo user = null;

            if (string.IsNullOrEmpty(token))
            {//控台的认证方式
                var key   = GetHeaderValue("key");
                var suser = GetHeaderValue("user");
                CashbagConfigurationElement setting = SettingSection.GetInstances().Cashbag;
                if (key == "1439e30938174d75a2360e4e3d3c6094" && !string.IsNullOrEmpty(suser))
                {
                    user = new CurrentUserInfo()
                    {
                        CashbagCode = setting.CashbagCode, CashbagKey = setting.CashbagKey, OperatorName = suser, OperatorAccount = suser
                    };
                }
            }
            else
            {
                Logger.WriteLog(LogType.DEBUG, token);
                var result = UserAuthResult <CurrentUserInfo> .Current(token);

                if (result != null)
                {
                    user = result.UserInfo;
                }
            }

            if (user != null)
            {
                AuthManager.SaveUser(user);
            }
            else
            {
                throw new NotAuthException("账户登录超时,请重新登录");
            }
            return(null);
        }
Ejemplo n.º 4
0
 public static List <CurrentUserInfo> GetOnLineUserInfo()
 {
     return(UserAuthResult <CurrentUserInfo> .FindAll().Select(p => p.UserInfo).ToList());
 }
Ejemplo n.º 5
0
    public UserAuthResult UserAuth(string ProvinceID, string SPID, string TimeStamp, string CustID, string UserAccount, string PhoneNum, string Password)
    {
        UserAuthResult Result = new UserAuthResult();
        Result.ErrorDescription = "初始";
        Result.ProvinceID = "35";
        Result.UserDetailInfo = null;
        Result.SubscriptionRecords = null;
        Result.ExtendField = "";

        Result.Result = CommonBizRules.CheckIPLimit(SPID, HttpContext.Current.Request.UserHostAddress, this.Context, out Result.ErrorDescription);
        if (Result.Result != 0)
        {
            return Result;
        }

        //接口访问权限判断
        Result.Result = CommonBizRules.CheckInterfaceLimit(SPID, "UserAuth", this.Context, out Result.ErrorDescription);
        if (Result.Result != 0)
        {
            return Result;
        }

        try
        {
            if (CommonUtility.IsEmpty(ProvinceID))
            {

                Result.Result = ErrorDefinition.BT_IError_Result_InValidProvinceID_Code;
                Result.ErrorDescription = ErrorDefinition.BT_IError_Result_InValidProvinceID_Msg + ",不能为空";
                return Result;
            }

            if (ProvinceID.Length != ConstDefinition.Length_ProvinceID)
            {
                Result.Result = ErrorDefinition.BT_IError_Result_InValidProvinceID_Code;
                Result.ErrorDescription = ErrorDefinition.BT_IError_Result_InValidProvinceID_Msg + ",长度有误";
                return Result;
            }

            ProvinceInfoManager proObject = new ProvinceInfoManager();
            object proDataObject = proObject.GetProvinceData(this.Context);
            if (proObject.GetPropertyByProvinceID(ProvinceID, "ProvinceCode", proDataObject) == "")
            {
                Result.Result = ErrorDefinition.BT_IError_Result_InValidProvinceID_Code;
                Result.ErrorDescription = ErrorDefinition.BT_IError_Result_InValidProvinceID_Msg + "无该省代码";
                return Result;
            }

            if (CommonUtility.IsEmpty(SPID))
            {
                Result.Result = ErrorDefinition.BT_IError_Result_InValidSPID_Code;
                Result.ErrorDescription = ErrorDefinition.BT_IError_Result_InValidSPID_Msg + ",不能为空";
                return Result;
            }

            if (SPID.Length != ConstDefinition.Length_SPID)
            {
                Result.Result = ErrorDefinition.BT_IError_Result_InValidSPID_Code;
                Result.ErrorDescription = ErrorDefinition.BT_IError_Result_InValidSPID_Msg + ",长度有误";
                return Result;
            }

            if (!CommonUtility.IsEmpty(CustID))
            {
                if (CustID.Length != ConstDefinition.Length_CustID)
                {
                    Result.Result = ErrorDefinition.BT_IError_Result_InValidCustID_Code;
                    Result.ErrorDescription = ErrorDefinition.BT_IError_Result_InValidCustID_Msg + ",长度有误";
                    return Result;
                }
            }

            if (!CommonUtility.IsEmpty(UserAccount))
            {
                if (UserAccount.Length < ConstDefinition.Length_Min_UserAccount)
                {
                    Result.Result = ErrorDefinition.BT_IError_Result_InValidUserAccount_Code;
                    Result.ErrorDescription = ErrorDefinition.BT_IError_Result_InValidUserAccount_Msg + ",长度有误";
                    return Result;
                }

                if (UserAccount.Length > ConstDefinition.Length_Max_UserAccount)
                {
                    Result.Result = ErrorDefinition.BT_IError_Result_InValidUserAccount_Code;
                    Result.ErrorDescription = ErrorDefinition.BT_IError_Result_InValidUserAccount_Msg + ",长度有误";
                    return Result;
                }
            }

            if (!CommonUtility.IsEmpty(PhoneNum))
            {
                string phone = "";
                if (!CommonBizRules.PhoneNumValid(this.Context, PhoneNum, out phone))
                {
                    Result.Result = ErrorDefinition.BT_IError_Result_InValidPhoneNum_Code;
                    Result.ErrorDescription = ErrorDefinition.BT_IError_Result_InValidPhoneNum_Msg;
                    return Result;
                }
                PhoneNum = phone;
            }

            if (CommonUtility.IsEmpty(Password))
            {

                Result.Result = ErrorDefinition.BT_IError_Result_InValidPassword_Code;
                Result.ErrorDescription = ErrorDefinition.BT_IError_Result_InValidPassword_Msg + ",不能为空";
                return Result;
            }

            string EncryptedPassword = CryptographyUtil.Encrypt(Password);
            string t_password = Password;
            Password = "";

            Result.Result = UserInfoClass.UserInfoQueryV2(ProvinceID, SPID, UserAccount, CustID, PhoneNum, Password, out Result.ErrorDescription, out Result.UserDetailInfo, out Result.SubscriptionRecords);

            StringBuilder msg = new StringBuilder();
            msg.Append("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r\n\r\n");
            msg.Append("客户信息平台密码鉴权认证接口 " + DateTime.Now.ToString("u") + "\r\n");
            msg.Append(";SPID - " + SPID);
            msg.Append(";EncryptedPassword - " + EncryptedPassword);
            msg.Append("\r\n");
            msg.Append("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r\n");

            BTUCenterInterfaceLog.CenterForBizTourLog("UserAuth", msg);

            if (Result.UserDetailInfo != null)
            {
                Result.Result = UserInfoClass.ValidUserPassword(Result.UserDetailInfo.CustID, EncryptedPassword);
                if (Result.Result == 0)
                {
                    Result.ErrorDescription = "认证通过";
                }
                else
                {
                    Result.UserDetailInfo = new UserInfo();
                    Result.ErrorDescription = "认证未通过";
                }
            }
            else
            {
                Result.ErrorDescription = "无此用户";
            }
        }
        catch (Exception e)
        {
            Result.Result = ErrorDefinition.IError_Result_System_UnknowError_Code;
            Result.ErrorDescription = ErrorDefinition.IError_Result_System_UnknowError_Msg + e.Message;
        }
        finally
        {
            try
            {
                #region WriteLog
                StringBuilder msg = new StringBuilder();
                msg.Append("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r\n\r\n");
                msg.Append("密码认证鉴权接口 " + DateTime.Now.ToString("u") + "\r\n");
                msg.Append("ProvinceID - " + ProvinceID);
                msg.Append(";SPID - " + SPID);
                msg.Append(";UserAccount - " + UserAccount);
                msg.Append(";CustID - " + CustID);
                msg.Append(";Password - " + Password);
                msg.Append(";TimeStamp - " + TimeStamp + "\r\n");

                msg.Append("处理结果 - " + Result.Result);
                msg.Append("; 错误描述 - " + Result.ErrorDescription);
                msg.Append("; ProvinceID - " + Result.ProvinceID);
                msg.Append("; ExtendField - " + Result.ExtendField + "\r\n");
                //记录UserDetailInfo
                UserInfoClass.WriteLogForUserInfo(Result.UserDetailInfo, ref msg);

                if (Result.SubscriptionRecords != null)
                    if (Result.SubscriptionRecords.Length > 0)
                    {
                        msg.Append("SubScriptionRecords: \r\n");
                        for (int i = 0; i < Result.SubscriptionRecords.Length; i++)
                        {
                            msg.Append("CustID - " + Result.SubscriptionRecords[i].CustID);
                            msg.Append("UserAccount - " + Result.SubscriptionRecords[i].UserAccount);
                            msg.Append("SubscribeStyle - " + Result.SubscriptionRecords[i].SubscribeStyle);
                            msg.Append("ServiceID - " + Result.SubscriptionRecords[i].ServiceID);
                            msg.Append("ServiceName - " + Result.SubscriptionRecords[i].ServiceName);
                            msg.Append("StartTime - " + Result.SubscriptionRecords[i].StartTime);
                            msg.Append("EndTime - " + Result.SubscriptionRecords[i].EndTime);
                            msg.Append("TransactionID - " + Result.SubscriptionRecords[i].TransactionID);

                            msg.Append("\r\n");
                        }
                    }
                msg.Append("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r\n");

                BTUCenterInterfaceLog.CenterForBizTourLog("UserAuth", msg);
                #endregion

                CommonBizRules.WriteDataLog(SPID, CustID, UserAccount, Result.Result,
                    Result.ErrorDescription, PhoneNum, "UserAuth");
            }
            catch { }
        }

        return Result;
    }
Ejemplo n.º 6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="user"></param>
        /// <param name="action"></param>
        /// <param name="loginIp"></param>
        /// <param name="businessmanType">0:采购,1,后台</param>
        /// <returns></returns>
        public UserAuthResult <CurrentUserInfo> Login(LoginParames user, System.Action <string> action)
        {
            var bussinessMan = businessmanRepository.FindAll(p => p.Code.ToUpper() == user.Code.ToUpper()).FirstOrDefault();

            if (bussinessMan == null)
            {
                throw new AuthException("没有找到商户号为" + user.Code + "的商户信息");
            }
            if (user.BusinessmanType == 0 && (bussinessMan is Supplier || bussinessMan is Carrier))
            {
                throw new AuthException("没有找到商户号为" + user.Code + "的商户信息。");
            }
            if (user.BusinessmanType == 1 && bussinessMan is Buyer)
            {
                throw new AuthException("没有找到商户号为" + user.Code + "的商户信息!");
            }
            if (!bussinessMan.IsEnable)
            {
                throw new AuthException("此商户号已经被冻结,请联系管理员");
            }
            var oper = bussinessMan.GetOperatorByPasswordAndAccount(user.Account.Trim(), user.Password);

            if (oper == null)
            {
                throw new AuthException("用户名或密码错误");
            }
            if (oper.OperatorState == Common.Enums.EnumOperatorState.Frozen)
            {
                throw new AuthException("该用户账号已被冻结");
            }
            if (user.BusinessmanType == 1 && oper.IsAdmin == false && oper.Role == null)
            {
                throw new AuthException("该帐号没有权限登录,请联系管理员!");
            }
            var currentUserInfo = new CurrentUserInfo()
            {
                Type            = bussinessMan.GetType().BaseType.Name,
                OperatorAccount = oper.Account,
                Code            = bussinessMan.Code,
                BusinessmanName = bussinessMan.Name,
                CashbagCode     = bussinessMan.CashbagCode,
                CashbagKey      = bussinessMan.CashbagKey,
                OperatorName    = oper.Realname,
                OperatorPhone   = oper.Phone,
                IsAdmin         = oper.IsAdmin,
                SettingInfo     = new SystemSettingInfo()
                {
                    SmsPrice = SettingSection.GetInstances().Sms.SmsPrice
                }
            };

            if (bussinessMan is Supplier)
            {
                currentUserInfo.CarrierCode = (bussinessMan as Supplier).CarrierCode;
            }
            if (bussinessMan is Buyer)
            {
                currentUserInfo.ContactName = (bussinessMan as Buyer).ContactName;
                currentUserInfo.Phone       = (bussinessMan as Buyer).Phone;
                currentUserInfo.CarrierCode = (bussinessMan as Buyer).CarrierCode;
            }
            if (bussinessMan is Carrier)
            {
                currentUserInfo.ContactName = oper.Realname;
            }
            LoginLog loginLog = new LoginLog
            {
                Code      = bussinessMan.Code,
                Account   = oper.Account,
                LoginIP   = user.LoginIP,
                LoginDate = System.DateTime.Now
            };
            var ur = new UserAuthResult <CurrentUserInfo>(currentUserInfo);

            ur.Save(p =>
            {
                action(currentUserInfo.GetIdentity());
            });
            //引发领域事件
            DomainEvents.Raise(new UserLoginEvent()
            {
                User = ur.UserInfo, LoginLog = loginLog
            });

            return(ur);
        }
Ejemplo n.º 7
0
 public CurrentUserInfo GetCurrentUserByToken(string token)
 {
     return(UserAuthResult <CurrentUserInfo> .Current(token).UserInfo);
 }
Ejemplo n.º 8
0
        public void LogoffByIdentity(string identity)
        {
            var token = UserAuthResult <CurrentUserInfo> .GetTokenByIdentity(identity);

            Logoff(token);
        }