public static IAppBuilder UseRedditAuthentication(this IAppBuilder app,
            RedditAuthenticationOptions options)
        {
            if (app == null)
                throw new ArgumentNullException(nameof(app));
            if (options == null)
                throw new ArgumentNullException(nameof(options));

            app.Use(typeof(RedditAuthenticationMiddleware), app, options);

            return app;
        }
示例#2
0
 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;
 }
示例#3
0
        public static IAppBuilder UseRedditAuthentication(this IAppBuilder app,
                                                          RedditAuthenticationOptions options)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            app.Use(typeof(RedditAuthenticationMiddleware), app, options);

            return(app);
        }
示例#4
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);
        }