public async Task <ActionResult> Index()
        {
            var currentUserName = User.Identity.Name;
            var userJson        = _apiDataCacheService.GetApiData(currentUserName, ApiStorageConstants.APIDATA_KEY_USER);

            if (userJson == null)
            {
                // get the data from the API
                var client = CreateClient();
                var user   = await client.User.Current();

                _apiDataCacheService.StoreApiData(currentUserName, ApiStorageConstants.APIDATA_KEY_USER, user);

                return(Json(User, JsonRequestBehavior.AllowGet));
            }

            return(Content(userJson, "application/json", Encoding.UTF8));
        }
示例#2
0
        public void ConfigureAuth(IAppBuilder app)
        {
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext <ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext <ApplicationSignInManager>(ApplicationSignInManager.Create);

            //// Enable the application to use a cookie to store information for the signed in user
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath          = new PathString("/Account/Login"),
                Provider           = new CookieAuthenticationProvider
                {
                    // Enables the application to validate the security stamp when the user logs in.
                    // This is a security feature which is used when you change a password or add an external login to your account.
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity <ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(20),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                }
            });
            // Use a cookie to temporarily store information about a user logging in with a third party login provider
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
            app.UseOAuthBearerTokens(OAuthOptions);

            var options = new GitHubAuthenticationOptions
            {
                // TODO: move these to an external config file
                ClientId     = WebApp.Configuration.Current().GetClientId(),
                ClientSecret = WebApp.Configuration.Current().GetClientSecret(),
                Provider     = new GitHubAuthenticationProvider
                {
#pragma warning disable 1998
                    OnAuthenticated = async context =>
                    {
                        try
                        {
                            _apiDataCacheService.StoreApiData(context.UserName, ApiStorageConstants.APIDATA_KEY_APITOKEN, context.AccessToken);
                            // TODO: prefetch some data?
                        }
                        catch (Exception e)
                        {
                            throw;
                        }
                    }
#pragma warning restore 1998
                },
            };

            options.Scope.Add("repo");
            options.Scope.Add("user");
            options.Scope.Add("user:email");
            app.UseGitHubAuthentication(options);
        }