Example #1
0
        public APIAppUserToken GetTokenInfo(Guid token)
        {
            APIAppUserToken result = new APIAppUserToken();

            try
            {
                AppUserToken appUserToken = db.AppUserTokens.First(t => t.Token == token);
                if (appUserToken != null)
                {     //Token is Exist
                    if (appUserToken.ExpireDate < DateTime.Now)
                    { //Token is Expired
                        throw new Exception("Token is Expired");
                    }
                    else
                    {//Token is Valid
                        result = TypesMapper.APITokenAdapter.fromToken(appUserToken);
                    }
                }
                else
                {//Token is not Exist
                    throw new Exception("Token is not exist");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Token is not exist");
            }

            return(result);
        }
Example #2
0
        private async void btnGetTokenInfo_ClickAsync(object sender, EventArgs e)
        {
            Initialize();
            APIAppUserToken token = await uow.TokenService.GetTokenInfo();

            MessageBox.Show("Expire Date: " + token.ExpireDate.ToShortDateString());
        }
Example #3
0
        private async void btnGenerateNewToken_Click(object sender, EventArgs e)
        {
            appGUID = Guid.Parse(txtAppID.Text);

            conf.URL      = txtURL.Text;
            conf.UserName = txtUser.Text;
            conf.Password = txtPassword.Text;
            conf.AppGUID  = txtAppID.Text;
            uow           = new UnitOfWork(conf);
            try
            {
                APIAppUserToken token = await uow.TokenService.GetNewToken();

                conf.URL      = txtURL.Text;
                conf.UserName = txtUser.Text;
                conf.Password = txtPassword.Text;
                conf.AppGUID  = txtAppID.Text;
                conf.Token    = token.Token.ToString();
                uow           = new UnitOfWork(conf);

                txtToken.Text = token.Token.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Example #4
0
        public async Task <APIAppUserToken> GetNewToken()
        {
            APIAppUserToken result       = new APIAppUserToken();
            HttpClient      client       = new HttpClient();
            string          resultstring = await client.GetStringAsync(hostconfig.URL + "/api/apptokens/GetNewToken?appGuid=" + hostconfig.AppGUID + "&userName="******"&password=" + hostconfig.Password);

            result = JsonConvert.DeserializeObject <APIAppUserToken>(resultstring);
            return(result);
        }
Example #5
0
        public async Task <APIAppUserToken> GetTokenInfo()
        {
            APIAppUserToken result       = new APIAppUserToken();
            HttpClient      client       = new HttpClient();
            string          resultstring = await client.GetStringAsync(hostconfig.URL + "/api/apptokens/GetTokenInfo?token=" + hostconfig.Token);

            result = JsonConvert.DeserializeObject <APIAppUserToken>(resultstring);
            return(result);
        }
Example #6
0
        public static APIAppUserToken fromToken(AppUserToken sourceToken)
        {
            APIAppUserToken result = new APIAppUserToken();

            result.AspNetUserID = (string)sourceToken.AspNetUserID;
            result.AppGUID      = sourceToken.App.GUID;
            result.CreateDate   = (DateTime)sourceToken.CreateDate;
            result.ExpireDate   = (DateTime)sourceToken.ExpireDate;
            result.Token        = (Guid)sourceToken.Token;

            return(result);
        }
Example #7
0
        public APIAppUserToken GetNewToken(Guid appGuid, string userName, string password)
        {
            APIAppUserToken   apiToken = new APIAppUserToken();
            AppUserToken      token    = new AppUserToken();
            List <AspNetUser> usrs     = db.AspNetUsers.Where(u => u.UserName == userName).ToList();

            if (usrs.Count > 0)
            {//User is exist
                //Validateing Password
                if (VerifyHashedPassword(usrs[0].PasswordHash, password))
                {//Validate App ID & Key
                    App app = db.Apps.First(a => a.GUID == appGuid);
                    if (app == null)
                    {//App not Found
                        throw new Exception("App not Found");
                    }
                    else
                    {//Create New Token
                        token.AppID        = app.ID;
                        token.AspNetUserID = usrs[0].Id;
                        token.CreateDate   = DateTime.Now;
                        token.ExpireDate   = DateTime.Now.AddDays(180);
                        token.Token        = Guid.NewGuid();
                        db.AppUserTokens.Add(token);
                        db.SaveChanges();
                        apiToken = TypesMapper.APITokenAdapter.fromToken(token);
                    }
                }
                else
                {//Wrong Password
                    throw new Exception("Wrong password");
                }
            }
            else
            {//User Not Exist
                throw new Exception("User not found");
            }
            return(apiToken);
        }