Ejemplo n.º 1
0
        public async Task <LoginResult> Login()
        {
            //List of standard Payload claims https://en.wikipedia.org/wiki/JSON_Web_Token#Standard_fields
            var ParametersByte = await Request.GetRawBodyBytesAsync();

            var LoginParameters = RestApiDataStore.GetObjectsFromByteArray <LoginParameters>(ParametersByte);

            if (LoginParameters.Username == "Joche" && LoginParameters.Password == "123")
            {
                JwtPayload InitialPayload;
                InitialPayload = new JwtPayload {
                    { JwtRegisteredClaimNames.NameId, LoginParameters.Username },
                    { JwtRegisteredClaimNames.Iat, JwtHelper.ConvertToUnixTime(DateTime.Now).ToString() },
                    { JwtRegisteredClaimNames.Iss, Issuer },
                };

                var StringToken = JwtHelper.GenerateToken(Key, InitialPayload);
                return(new LoginResult()
                {
                    Authenticated = true, Token = StringToken
                });
            }
            else
            {
                return new LoginResult()
                       {
                           Authenticated = false, Token = ""
                       }
            };
        }
    }
Ejemplo n.º 2
0
        public async Task<LoginResult> Login()
        {
            //List of standard Payload claims https://en.wikipedia.org/wiki/JSON_Web_Token#Standard_fields

            byte[] ParametersByte = await Request.GetRawBodyBytesAsync();

            LoginParameters LoginParameters = RestApiDataStore.GetObjectsFromByteArray<LoginParameters>(ParametersByte);
            string Database;
            string Server;
            Employee User = null;
            try
            {

                string UserName = WebUtility.UrlDecode(LoginParameters.Username);
                string Password = WebUtility.UrlDecode(LoginParameters.Password);
               
                Server = WebUtility.UrlDecode(LoginParameters.Server);

                Database = WebUtility.UrlDecode(LoginParameters.Database);
                UnitOfWork UoW = XpoProxyHelper.GetUnitOfWork(Database, Server);

                User = UoW.FindObject<Employee>(new BinaryOperator("UserName", UserName));
               
              
                if (User == null)
                {
                    return new LoginResult() { Authenticated = false, Token = "" };
                }
                if (!User.ComparePassword(Password))
                {

                    return new LoginResult() { Authenticated = false, Token = "" }; //TODO invalid password

                }

               

            }
            catch (Exception exception)
            {
                return new LoginResult() { Authenticated = false, Token = "", ErrorMessage= exception.Message };
            }
      

             JwtPayload InitialPayload;
                InitialPayload = new JwtPayload
                {
                    { JwtRegisteredClaimNames.NameId, LoginParameters.Username },
                    { JwtRegisteredClaimNames.Iat, JwtHelper.ConvertToUnixTime(DateTime.Now).ToString() },
                    { JwtRegisteredClaimNames.Iss, Issuer },
                    { "DatabaseId", Database },
                    { "ServerId", Server },
                };

                var StringToken = JwtHelper.GenerateToken(Key, InitialPayload);
                return new LoginResult() { Authenticated = true, Token = StringToken, CurrentUserId = User?.Oid.ToString()};
           
        }