Esempio n. 1
0
        public async Task <ActionResult <GetMeResult> > Get()
        {
            var userId = Guid.Parse(userManager.GetUserId(User));
            var result = await authorizationService.GetJwtTokenForUser(userId);

            if (result.IsT1)
            {
                logger.LogWarning($"Correct JWT but user not found userId: {userId}");
                return(Forbid("Bearer"));
            }

            var(accessToken, user) = result.AsT0;
            var plainResult = GenerateResponse(user, accessToken);
            var claims      = await userManager.GetClaimsAsync(user);

            var roles = await userManager.GetRolesAsync(user);

            claims = claims.Concat(roles.Select(r => new Claim(ClaimTypes.Role, r))).ToList();

            var totalResult = new GetMeResult
            {
                Id        = plainResult.Id,
                FirstName = plainResult.FirstName,
                Email     = plainResult.Email,
                Token     = plainResult.Token,
                StudentId = plainResult.StudentId,
                Claims    = claims.GroupBy(c => c.Type).ToDictionary(c => c.Key, g => g.Select(c => c.Value).ToArray())
            };

            return(totalResult);
        }
Esempio n. 2
0
        private void backgroundLoginWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (Session == null)
            {
                String   vmosoHost = Properties.Settings.Default.VmosoHost;
                FileInfo hostFile  = new FileInfo(dataFolder + "/host");
                if (hostFile.Exists)
                {
                    vmosoHost = File.ReadAllText(hostFile.FullName);
                    ServicePointManager.ServerCertificateValidationCallback += (sdr, certificate, chain, sslPolicyErrors) => true;
                }

                String   vmosoCid = Properties.Settings.Default.VmosoCid;
                FileInfo cidFile  = new FileInfo(dataFolder + "/cid");
                if (cidFile.Exists)
                {
                    vmosoCid = File.ReadAllText(cidFile.FullName);
                }

                log.Info("Creating session for host " + vmosoHost);
                Session = new VmosoSession(vmosoHost, vmosoCid);
                //if (Properties.Settings.Default.UseProxy) {
                //	Session.setProxy(Properties.Settings.Default.ProxyHost, Properties.Settings.Default.ProxyPort, Properties.Settings.Default.ProxyUser, Properties.Settings.Default.ProxyPassword);
                //}

                try {
                    log.Info("Login with username " + Properties.Settings.Default.VmosoUsername);
                    Session.Login(Properties.Settings.Default.VmosoUsername, Properties.Settings.Default.VmosoPassword);
                } catch (ApiException ex) {
                    log.Error("Login error", ex);
                    Exception ex2 = new Exception(Properties.Resources.error_login, ex);
                    e.Result = ex2;
                }
            }

            try {
                log.Info("Getting user info for username " + Properties.Settings.Default.VmosoUsername);
                UserApi userApi = new UserApi(Session.GetApiClient());

                GetMeResult result = userApi.GetMe();
                if (result.Hdr.Rc == 0)
                {
                    log.Info("User " + result.DisplayRecord.DisplayName + " logged");
                    e.Result = result.DisplayRecord.DisplayName;
                }
                else
                {
                    Exception ex2 = new Exception(Properties.Resources.error_login);
                    log.Error("Vmoso error getting user info. Rc=" + result.Hdr.Rc);
                    e.Result = ex2;
                }
            } catch (Exception ex) {
                Exception ex2 = new Exception(Properties.Resources.error_login);
                log.Error("Error getting user info", ex);
                e.Result = ex2;
            }
        }