Example #1
0
        static async Task <int> Main(string[] args)
        {
            var collection = new ServiceCollection();

            collection.AddDbContext <ApplicationDbContext>();

            collection.AddIdentity <ApplicationUser, IdentityRole>(o =>
            {
                o.Password.RequireNonAlphanumeric = false;
                o.Password.RequireUppercase       = false;
            })
            .AddEntityFrameworkStores <ApplicationDbContext>();
            collection.AddLogging();
            var sp = collection.BuildServiceProvider();

            var seed = Creds.RandomUser();
            var usr  = seed.Email;

            if (args.Length > 0)
            {
                usr = args[0];
            }
            Console.WriteLine($"username: {seed.Email} pwd: {seed.Password}");

            var now    = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            var access = await Token.GetAccess(sp, now, seed);

            Console.WriteLine(access.Accesstoken);
            return(0);
        }
Example #2
0
        public static async Task <Access> GetAccess(
            ServiceProvider sp,
            long now,
            Creds creds)
        {
            var access = await TokenStore.Read(creds.Email);

            if (access == null)
            {
                access = await FetchToken(sp, now, creds);

                await TokenStore.Write(creds.Email, access);
            }

            var refreshThreshold = DateTimeOffset.Now.Subtract(TimeSpan.FromMinutes(5));

            if (access.ValidTo < refreshThreshold.ToUnixTimeMilliseconds())
            {
                access = await Refresh(access);

                await TokenStore.Write(creds.Email, access);
            }

            return(access);
        }
Example #3
0
        static Task <Access> RandomAccess(
            long now,
            ServiceProvider sp
            )
        {
            var creds = Creds.RandomUser();

            return(Token.FetchToken(sp, now, creds));
        }
Example #4
0
        public static async Task <Access> FetchToken(
            ServiceProvider sp,
            long now,
            Creds creds
            )
        {
            using var scope  = sp.CreateScope();
            using var usrmgr = scope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >();
            var user = new ApplicationUser()
            {
                Id       = Guid.NewGuid().ToString(),
                UserName = Guid.NewGuid().ToString(),
                Email    = creds.Email
            };
            await usrmgr.CreateAsync(user, creds.Password);

            var idpclient = new HttpClient();
            var idpreply  = await idpclient.RequestPasswordTokenAsync(
                new PasswordTokenRequest
            {
                Address = "https://ids.ego/connect/token",

                ClientId     = "dev",
                ClientSecret = "dev",
                Scope        = "openid bookings publish offline_access",

                UserName = creds.Email,
                Password = creds.Password
            });

            if (idpreply.IsError)
            {
                throw new Exception(idpreply.Error, idpreply.Exception);
            }

            return(new Access
            {
                Accesstoken = idpreply.AccessToken,
                Refreshtoken = idpreply.RefreshToken,
                ValidTo = now + (idpreply.ExpiresIn * 1000)
            });
        }