Exemplo n.º 1
0
        public static IApplicationBuilder UseRedditAuthentication(
            [NotNull] this IApplicationBuilder app,
            [NotNull] Action <RedditAuthenticationOptions> configuration)
        {
            var options = new RedditAuthenticationOptions();

            configuration(options);

            return(app.UseRedditAuthentication(options));
        }
        private static RedditAuthenticationOptions GetRedditOptions()
        {
            var options = new RedditAuthenticationOptions
            {
                ClientId     = ConfigurationManager.AppSettings["reddit.auth.clientId"],
                ClientSecret = ConfigurationManager.AppSettings["reddit.auth.clientSecret"]
            };

            options.Scope.Clear();
            options.Scope.Add("identity");
            options.Scope.Add("mysubreddits");
            return(options);
        }
        public TenantMiddleware(
            RequestDelegate next,
            TenantService tenantService,
            IAuthenticationSchemeProvider oauthProvider,
            IMemoryCache memoryCache,
            IdentityServerOptions identityServerOptions,

            IOptionsMonitor <AmazonAuthenticationOptions> amazonOptions,
            IOptionsMonitor <FacebookOptions> facebookOptions,
            IOptionsMonitor <GitHubOptions> githubOptions,
            IOptionsMonitor <GitterAuthenticationOptions> gitterOptions,
            IOptionsMonitor <GoogleOptions> googleOptions,
            IOptionsMonitor <InstagramAuthenticationOptions> instagramOptions,
            IOptionsMonitor <LinkedInAuthenticationOptions> linkedinOptions,
            IOptionsMonitor <MicrosoftAccountOptions> microsoftOptions,
            IOptionsMonitor <PaypalAuthenticationOptions> paypalOptions,
            IOptionsMonitor <QQOptions> qqOptions,
            IOptionsMonitor <RedditAuthenticationOptions> redditOptions,
            IOptionsMonitor <SalesforceAuthenticationOptions> salesforceOptions,
            IOptionsMonitor <TwitterOptions> twitterOptions,
            IOptionsMonitor <VisualStudioAuthenticationOptions> visualstudioOptions,
            IOptionsMonitor <WeiboOptions> weiboOptions,
            IOptionsMonitor <WeixinOptions> weixinOptions,
            IOptionsMonitor <WordPressAuthenticationOptions> wordpressOptions
            )
        {
            _next                  = next;
            _tenantService         = tenantService;
            _oauthProvider         = oauthProvider;
            _memoryCache           = memoryCache;
            _identityServerOptions = identityServerOptions;

            _amazonOptions       = amazonOptions.CurrentValue;
            _facebookOptions     = facebookOptions.CurrentValue;
            _githubOptions       = githubOptions.CurrentValue;
            _gitterOptions       = gitterOptions.CurrentValue;
            _googleOptions       = googleOptions.CurrentValue;
            _instagramOptions    = instagramOptions.CurrentValue;
            _linkedinOptions     = linkedinOptions.CurrentValue;
            _microsoftOptions    = microsoftOptions.CurrentValue;
            _paypalOptions       = paypalOptions.CurrentValue;
            _qqOptions           = qqOptions.CurrentValue;
            _redditOptions       = redditOptions.CurrentValue;
            _salesforceOptions   = salesforceOptions.CurrentValue;
            _twitterOptions      = twitterOptions.CurrentValue;
            _visualstudioOptions = visualstudioOptions.CurrentValue;
            _weiboOptions        = weiboOptions.CurrentValue;
            _weixinOptions       = weixinOptions.CurrentValue;
            _wordpressOptions    = wordpressOptions.CurrentValue;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Adds the <see cref="RedditAuthenticationMiddleware"/> middleware to the specified
        /// <see cref="IApplicationBuilder"/>, which enables Reddit authentication capabilities.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
        /// <param name="options">A <see cref="RedditAuthenticationOptions"/> that specifies options for the middleware.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IApplicationBuilder UseExtendedRedditAuthentication(
            this IApplicationBuilder app,
            RedditAuthenticationOptions options)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            return(app.UseMiddleware <ExtendedRedditAuthenticationMiddleware>(Options.Create(options)));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Adds the <see cref="ExtendedRedditAuthenticationMiddleware"/> middleware to the specified
        /// <see cref="IApplicationBuilder"/>, which enables Reddit authentication capabilities.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
        /// <param name="configuration">An action delegate to configure the provided <see cref="RedditAuthenticationOptions"/>.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IApplicationBuilder UseRedditAuthentication(
            this IApplicationBuilder app,
            Action <RedditAuthenticationOptions> configuration)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var options = new RedditAuthenticationOptions();

            configuration(options);

            return(app.UseMiddleware <ExtendedRedditAuthenticationMiddleware>(Options.Create(options)));
        }
Exemplo n.º 6
0
 public static IApplicationBuilder UseRedditAuthentication(
     [NotNull] this IApplicationBuilder app,
     [NotNull] RedditAuthenticationOptions options)
 {
     return(app.UseMiddleware <RedditAuthenticationMiddleware>(options));
 }
Exemplo n.º 7
0
        private void ConfigureAuth(IAppBuilder app)
        {
            app.CreatePerOwinContext(ApplicationDbContext.Create);
            app.CreatePerOwinContext <ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext <ApplicationSignInManager>(ApplicationSignInManager.Create);

            var cookieOptions = new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath          = new PathString("/Auth/Login"),
                CookieName         = "bog", ExpireTimeSpan = new TimeSpan(10000, 0, 0, 0, 0),
                Provider           = new CookieAuthenticationProvider
                {
                    OnException = context => {
                        var x = context;
                    },
                    OnValidateIdentity = async context => {
                        var invalidateBySecurityStamp = SecurityStampValidator.OnValidateIdentity <ApplicationUserManager, ApplicationUser>(
                            validateInterval: TimeSpan.FromMinutes(15),
                            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager));
                        await invalidateBySecurityStamp.Invoke(context);

                        if (context.Identity == null || !context.Identity.IsAuthenticated)
                        {
                            return;
                        }
                        var newResponseGrant = context.OwinContext.Authentication.AuthenticationResponseGrant;
                        if (newResponseGrant != null)
                        {
                            newResponseGrant.Properties.IsPersistent = true;
                        }
                    },/*context =>
                       * {
                       * if (DateTime.Parse(context.Identity.FindFirst(c => c.Type == "urn:reddit:accessexpires").Value) < DateTime.UtcNow)
                       * {
                       *    context.Identity.RemoveClaim(context.Identity.Claims.Where(c => c.Type == "urn:reddit:accessexpires").First());
                       *    context.Identity.AddClaim(new System.Security.Claims.Claim("urn:reddit:accessexpires", DateTime.UtcNow.AddMinutes(5).ToString()));
                       *    //GetNewToken(context);
                       * }
                       * var newResponseGrant = context.OwinContext.Authentication.AuthenticationResponseGrant;
                       * if (newResponseGrant != null)
                       * {
                       *    newResponseGrant.Properties.IsPersistent = true;
                       * }
                       * return System.Threading.Tasks.Task.FromResult(0);
                       * },*/
                    OnApplyRedirect = ctx =>
                    {
                        //if (!IsAjaxRequest(ctx.Request))
                        //{
                        //    ctx.Response.Redirect(ctx.RedirectUri);
                        //}
                    }
                }
            };

            app.UseCookieAuthentication(cookieOptions);
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

            RedditAuthenticationOptions opts = new RedditAuthenticationOptions
            {
                ClientId     = System.Configuration.ConfigurationManager.AppSettings["RedditClientID"],
                ClientSecret = System.Configuration.ConfigurationManager.AppSettings["RedditClientSecret"],
                //CallbackPath = new PathString(System.Configuration.ConfigurationManager.AppSettings["RedditRedirectURI"]),
                UserAgent = "SnooNotes (by /u/meepster23)",
                Provider  = new Owin.Security.Providers.Reddit.Provider.RedditAuthenticationProvider()
                {
                    OnAuthenticated = context =>
                    {
                        context.Identity.AddClaim(new Claim("urn:reddit:refresh", context.RefreshToken));
                        context.Identity.AddClaim(new Claim("urn:reddit:accessexpires", DateTime.UtcNow.Add(context.ExpiresIn.HasValue ? context.ExpiresIn.Value : new TimeSpan(0, 50, 0)).ToString()));
                        context.Identity.AddClaim(new Claim("urn:reddit:scope", string.Join(",", context.Scope)));
                        //context.Identity = GetModeratedSubreddits(context.Identity as ClaimsIdentity);

                        return(System.Threading.Tasks.Task.FromResult(0));
                    }
                }
            };

            opts.Scope.Clear();
            opts.Scope.Add("identity");
            opts.Scope.Add("mysubreddits");
            //app.UseRedditAuthentication(opts);

            app.UseOpenIdConnectAuthentication(new Microsoft.Owin.Security.OpenIdConnect.OpenIdConnectAuthenticationOptions()
            {
                AuthenticationType         = "oidc",
                SignInAsAuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                Authority     = "http://localhost:5000",
                ClientId      = "mvc",
                ClientSecret  = "secret",
                ResponseType  = "code id_token",
                Scope         = "api1 offline_access openid"
                , RedirectUri = "http://localhost:44322/signin-oidc"
            });
        }