public static async Task CookieSignin(CookieSigningInContext context) { UserManager <Models.ApplicationUser> userManager = context.HttpContext.RequestServices.GetRequiredService <UserManager <Models.ApplicationUser> >(); SignInManager <Models.ApplicationUser> signinManager = context.HttpContext.RequestServices.GetRequiredService <SignInManager <Models.ApplicationUser> >(); RoleManager <IdentityRole> roleManager = context.HttpContext.RequestServices.GetRequiredService <RoleManager <IdentityRole> >(); ILoggerFactory logger = context.HttpContext.RequestServices.GetRequiredService <ILoggerFactory>(); IMemoryCache cache = context.HttpContext.RequestServices.GetRequiredService <IMemoryCache>(); Utilities.IAuthUtils authutils = context.HttpContext.RequestServices.GetRequiredService <Utilities.IAuthUtils>(); RedditSharp.RefreshTokenWebAgentPool agentPool = context.HttpContext.RequestServices.GetRequiredService <RedditSharp.RefreshTokenWebAgentPool>(); await agentPool.RemoveWebAgentAsync(context.Principal.Identity.Name); var user = await userManager.FindByNameAsync(context.Principal.Identity.Name); await authutils.UpdateModeratedSubredditsAsync(user); user = await userManager.FindByNameAsync(context.Principal.Identity.Name); var newPrincipal = await signinManager.CreateUserPrincipalAsync(user); if (user.HasWiki) { ((ClaimsIdentity)newPrincipal.Identity).AddClaim(new Claim("uri:snoonotes:haswiki", "true")); } if (user.HasConfig) { ((ClaimsIdentity)newPrincipal.Identity).AddClaim(new Claim("uri:snoonotes:hasconfig", "true")); } ((ClaimsIdentity)newPrincipal.Identity).AddClaim(new Claim("lastupdated", DateTime.UtcNow.ToString())); context.Principal = newPrincipal; }
public AuthUtilsTests() { ConfigurationBuilder builder = new ConfigurationBuilder(); builder.AddUserSecrets <AuthUtilsTests>(); Configuration = builder.Build(); var servs = new ServiceCollection(); servs.AddEntityFrameworkInMemoryDatabase() //eww, make microsoft fix this inmemorydatabase not disposing properly. .AddDbContext <ApplicationDbContext>(options => options.UseInMemoryDatabase(Guid.NewGuid().ToString())) .AddIdentity <ApplicationUser, IdentityRole>() .AddEntityFrameworkStores <ApplicationDbContext>(); serviceProvider = servs.BuildServiceProvider(); testUser = new ApplicationUser { Id = "testUser", UserName = Configuration["UserName"], RefreshToken = Configuration["UserRefreshToken"], TokenExpires = DateTime.UtcNow.AddMinutes(-5) }; agentPool = new RedditSharp.RefreshTokenWebAgentPool(Configuration["RedditClientID"], Configuration["RedditClientSecret"], Configuration["RedditRedirectURI"]) { DefaultRateLimitMode = RedditSharp.RateLimitMode.Burst, DefaultUserAgent = "SnooNotes (by Meepster23)" }; }
public ToolBoxNotesController(UserManager <ApplicationUser> userManager, DAL.INoteTypesDAL noteTypesDAL, Utilities.IAuthUtils authUtils, RedditSharp.RefreshTokenWebAgentPool agentPool, BLL.IToolBoxNotesBLL tbNotesBLL) { this.userManager = userManager; this.authUtils = authUtils; this.agentPool = agentPool; this.noteTypesDAL = noteTypesDAL; this.tbNotesBLL = tbNotesBLL; }
public BotBanBLL(DAL.IBotBanDAL botBanDAL, UserManager <ApplicationUser> userManager, Utilities.IAuthUtils authUtils, DAL.IYouTubeDAL youtubeDAL, RedditSharp.RefreshTokenWebAgentPool agentPool) { bbDAL = botBanDAL; this.userManager = userManager; this.authUtils = authUtils; ytDAL = youtubeDAL; this.agentPool = agentPool; }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { string connectionString = Configuration.GetConnectionString("SnooNotes"); var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name; // Add framework services. services.AddDbContext <ApplicationDbContext>(options => options.UseSqlServer(connectionString)); services.AddIdentity <ApplicationUser, IdentityRole>() .AddEntityFrameworkStores <ApplicationDbContext>() .AddDefaultTokenProviders(); services.AddScoped <IUserClaimsPrincipalFactory <ApplicationUser>, CustomUserClaimsPrincipalFactory>(); services.AddCors(opt => opt.AddPolicy("AllowAll", pol => pol.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod().AllowCredentials())); services.AddMvc(); services.Configure <IdentityOptions>(options => { options.User.RequireUniqueEmail = false; options.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromDays(150); options.Cookies.ApplicationCookie.SlidingExpiration = true; }); // Add application services. services.AddSingleton <IConfigurationRoot>(Configuration); services.AddTransient <IEmailSender, AuthMessageSender>(); services.AddTransient <ISmsSender, AuthMessageSender>(); services.AddTransient <SnooNotes.DAL.ISubredditDAL, SnooNotes.DAL.BaseSubredditDAL>(); services.AddTransient <SnooNotes.Utilities.IAuthUtils, SnooNotes.Utilities.BaseAuthUtils>(); var identServer = services.AddIdentityServer(options => { //options.Cors.CorsPolicyName = "AllowAll"; } ).AddSigningCredential("CN=SNIdentServer", System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser) .AddConfigurationStore(builder => builder.UseSqlServer(connectionString, options => options.MigrationsAssembly(migrationsAssembly))) .AddOperationalStore(builder => builder.UseSqlServer(connectionString, options => options.MigrationsAssembly(migrationsAssembly))) .AddAspNetIdentity <ApplicationUser>() .AddEndpoint <Controllers.CustomCheckSessionEndpoint>(IdentityServer4.Hosting.EndpointName.CheckSession) .Services.AddTransient <IdentityServer4.ResponseHandling.ITokenResponseGenerator, CustomTokenResponseGenerator>() .AddTransient <IdentityServer4.ResponseHandling.IAuthorizeResponseGenerator, CustomAuthorizeResponseGenerator>(); var webAgentPool = new RedditSharp.RefreshTokenWebAgentPool(Configuration["RedditClientID"], Configuration["RedditClientSecret"], Configuration["RedditRedirectURI"]); webAgentPool.DefaultRateLimitMode = RedditSharp.RateLimitMode.Burst; webAgentPool.DefaultUserAgent = "SnooNotes (by Meepster23)"; services.AddSingleton(webAgentPool); services.AddSingleton(new RedditSharp.WebAgentPool <string, RedditSharp.BotWebAgent>()); RedditSharp.WebAgent.DefaultUserAgent = "SnooNotes IdentityProvider (by Meepster23)"; RedditSharp.WebAgent.DefaultRateLimiter.Mode = RedditSharp.RateLimitMode.Burst; }
public AuthUtils(IConfigurationRoot config, UserManager <ApplicationUser> userManager, RoleManager <IdentityRole> roleManager, ILoggerFactory loggerFactory, DAL.ISubredditDAL subredditDAL, RedditSharp.RefreshTokenWebAgentPool agentPool, RedditSharp.WebAgentPool <string, RedditSharp.BotWebAgent> serviceAgentPool) : base(config, userManager, roleManager, loggerFactory, subredditDAL, agentPool, serviceAgentPool) { _userManager = userManager; //_logger = loggerFactory.CreateLogger<AuthUtils>(); _roleManager = roleManager; Configuration = config; subDAL = subredditDAL; this.agentPool = agentPool; }
public AccountController(UserManager <ApplicationUser> userManager, SignInManager <ApplicationUser> signInManager, ILoggerFactory loggerFactory, BLL.ISubredditBLL subredditBLL, Utilities.IAuthUtils authUtils, IMemoryCache memoryCache, RoleManager <IdentityRole> roleManager, RedditSharp.RefreshTokenWebAgentPool agentPool, RedditSharp.WebAgentPool <string, RedditSharp.BotWebAgent> serviceAgentPool, IConfigurationRoot configRoot) { subBLL = subredditBLL; _userManager = userManager; _signInManager = signInManager; _logger = loggerFactory.CreateLogger <AccountController>(); this.authUtils = authUtils; this.agentPool = agentPool; this.serviceAgentPool = serviceAgentPool; Configuration = configRoot; }
public AccountController( UserManager <ApplicationUser> userManager, SignInManager <ApplicationUser> signInManager, IEmailSender emailSender, ISmsSender smsSender, ILoggerFactory loggerFactory, IIdentityServerInteractionService interaction, IHttpContextAccessor httpContext, IClientStore clientStore, IConfigurationRoot config, RedditSharp.RefreshTokenWebAgentPool agentPool, SnooNotes.Utilities.IAuthUtils authUtils) { _userManager = userManager; _signInManager = signInManager; _emailSender = emailSender; _smsSender = smsSender; _logger = loggerFactory.CreateLogger <AccountController>(); _account = new AccountService(interaction, httpContext, clientStore); _config = config; _agentPool = agentPool; _authUtils = authUtils; }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var settings = new JsonSerializerSettings(); settings.ContractResolver = new SignalRContractResolver(); services.AddDbContext <ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SnooNotes"))); services.AddIdentity <ApplicationUser, IdentityRole>(options => options.Cookies.ApplicationCookie.AuthenticationScheme = "cookie") .AddEntityFrameworkStores <ApplicationDbContext>() .AddDefaultTokenProviders(); services.AddMvc().AddJsonOptions(opt => { var resolver = opt.SerializerSettings.ContractResolver; if (resolver != null) { var res = resolver as DefaultContractResolver; res.NamingStrategy = null; // <<!-- this removes the camelcasing } }); services.Configure <IdentityOptions>(options => { options.User.RequireUniqueEmail = false; options.Cookies.ApplicationCookie.AuthenticationScheme = "cookie"; options.Cookies.ApplicationCookie.ExpireTimeSpan = TimeSpan.FromDays(150); options.Cookies.ApplicationCookie.SlidingExpiration = true; }); var serializer = JsonSerializer.Create(settings); //services.Add( new ServiceDescriptor( typeof( JsonSerializer ), // provider => serializer, // ServiceLifetime.Transient ) ); // Add framework services. services.AddSingleton <IConfigurationRoot>(Configuration); services.AddSignalR(options => { options.Hubs.EnableDetailedErrors = true; }); services.AddSingleton <Signalr.ISnooNoteUpdates, Signalr.SnooNoteUpdates>(); services.AddScoped <DAL.IDirtbagDAL, DAL.DirtbagDAL>(); services.AddScoped <DAL.INotesDAL, DAL.NotesDAL>(); services.AddScoped <DAL.INoteTypesDAL, DAL.NoteTypesDAL>(); services.AddScoped <DAL.ISubredditDAL, DAL.SubredditDAL>(); services.AddScoped <DAL.IYouTubeDAL, DAL.YouTubeDAL>(); services.AddTransient <Utilities.IAuthUtils, SnooNotes.Utilities.AuthUtils>(); services.AddTransient <BLL.IDirtbagBLL, BLL.DirtbagBLL>(); services.AddTransient <BLL.INotesBLL, BLL.NotesBLL>(); services.AddTransient <BLL.INoteTypesBLL, BLL.NoteTypesBLL>(); services.AddTransient <BLL.ISubredditBLL, BLL.SubredditBLL>(); services.AddTransient <BLL.IToolBoxNotesBLL, BLL.ToolBoxNotesBLL>(); services.AddTransient <DAL.IBotBanDAL>((x) => { return(new DAL.BotBanDAL(new SqlConnection(Configuration.GetConnectionString("SnooNotes")), new NpgsqlConnection(Configuration.GetConnectionString("Sentinel")))); }); services.AddTransient <BLL.IBotBanBLL, BLL.BotBanBLL>(); var webAgentPool = new RedditSharp.RefreshTokenWebAgentPool(Configuration["RedditClientID"], Configuration["RedditClientSecret"], Configuration["RedditRedirectURI"]) { DefaultRateLimitMode = RedditSharp.RateLimitMode.Burst, DefaultUserAgent = "SnooNotes (by Meepster23)" }; services.AddSingleton(webAgentPool); services.AddSingleton(new RedditSharp.WebAgentPool <string, RedditSharp.BotWebAgent>()); RedditSharp.WebAgent.DefaultUserAgent = "SnooNotes (by Meepster23)"; RedditSharp.WebAgent.DefaultRateLimiter.Mode = RedditSharp.RateLimitMode.Burst; services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("SnooNotes")).UseActivator(new Hangfire.AspNetCore.AspNetCoreJobActivator((IServiceScopeFactory)services.BuildServiceProvider().GetService(typeof(IServiceScopeFactory))))); }