diff --git a/src/MusicStore/Controllers/AccountController.cs b/src/MusicStore/Controllers/AccountController.cs index e46723ec67..a1a243b0ae 100644 --- a/src/MusicStore/Controllers/AccountController.cs +++ b/src/MusicStore/Controllers/AccountController.cs @@ -282,7 +282,7 @@ namespace MusicStore.Controllers { // Request a redirect to the external login provider var redirectUrl = Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }); - var properties = Context.ConfigureExternalAuthenticationProperties(provider, redirectUrl); + var properties = SignInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl); return new ChallengeResult(provider, properties); } @@ -328,7 +328,7 @@ namespace MusicStore.Controllers public async Task ExternalLoginCallback(string returnUrl = null) { //https://github.com/aspnet/Identity/issues/216 - var loginInfo = await Context.GetExternalLoginInfo(); + var loginInfo = await SignInManager.GetExternalLoginInfoAsync(); if (loginInfo == null) { return RedirectToAction("Login"); @@ -371,7 +371,7 @@ namespace MusicStore.Controllers { // Get the information about the user from the external login provider //https://github.com/aspnet/Identity/issues/216 - var info = await Context.GetExternalLoginInfo(); + var info = await SignInManager.GetExternalLoginInfoAsync(); if (info == null) { return View("ExternalLoginFailure"); diff --git a/src/MusicStore/Controllers/ManageController.cs b/src/MusicStore/Controllers/ManageController.cs index f3746f2bbf..553663a017 100644 --- a/src/MusicStore/Controllers/ManageController.cs +++ b/src/MusicStore/Controllers/ManageController.cs @@ -294,7 +294,7 @@ namespace MusicStore.Controllers return View("Error"); } var userLogins = await UserManager.GetLoginsAsync(user, cancellationToken: Context.RequestAborted); - var otherLogins = Context.GetExternalAuthenticationTypes().Where(auth => userLogins.All(ul => auth.AuthenticationType != ul.LoginProvider)).ToList(); + var otherLogins = SignInManager.GetExternalAuthenticationTypes().Where(auth => userLogins.All(ul => auth.AuthenticationType != ul.LoginProvider)).ToList(); ViewBag.ShowRemoveButton = user.PasswordHash != null || userLogins.Count > 1; return View(new ManageLoginsViewModel { @@ -311,7 +311,7 @@ namespace MusicStore.Controllers { // Request a redirect to the external login provider to link a login for the current user var redirectUrl = Url.Action("LinkLoginCallback", "Manage"); - var properties = Context.ConfigureExternalAuthenticationProperties(provider, redirectUrl, User.Identity.GetUserId()); + var properties = SignInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl, User.Identity.GetUserId()); return new ChallengeResult(provider, properties); } @@ -325,7 +325,7 @@ namespace MusicStore.Controllers return View("Error"); } //https://github.com/aspnet/Identity/issues/216 - var loginInfo = await Context.GetExternalLoginInfo(User.Identity.GetUserId()); + var loginInfo = await SignInManager.GetExternalLoginInfoAsync(User.Identity.GetUserId()); if (loginInfo == null) { return RedirectToAction("ManageLogins", new { Message = ManageMessageId.Error }); diff --git a/src/MusicStore/Mocks/StartupSocialTesting.cs b/src/MusicStore/Mocks/StartupSocialTesting.cs index f0efd3f5a2..c9262243a9 100644 --- a/src/MusicStore/Mocks/StartupSocialTesting.cs +++ b/src/MusicStore/Mocks/StartupSocialTesting.cs @@ -40,8 +40,6 @@ namespace MusicStore //Note: ErrorPageOptions.ShowAll to be used only at development time. Not recommended for production. app.UseErrorPage(ErrorPageOptions.ShowAll); - app.SetDefaultSignInAsAuthenticationType("External"); - app.UseServices(services => { //If this type is present - we're on mono @@ -62,23 +60,40 @@ namespace MusicStore services.AddScoped(); // Configure DbContext - services.SetupOptions(options => - { - options.DefaultAdminUserName = configuration.Get("DefaultAdminUsername"); - options.DefaultAdminPassword = configuration.Get("DefaultAdminPassword"); - if (runningOnMono) - { - options.UseInMemoryStore(); - } - else - { - options.UseSqlServer(configuration.Get("Data:DefaultConnection:ConnectionString")); - } - }); + services.ConfigureOptions(options => + { + options.DefaultAdminUserName = configuration.Get("DefaultAdminUsername"); + options.DefaultAdminPassword = configuration.Get("DefaultAdminPassword"); + if (runningOnMono) + { + options.UseInMemoryStore(); + } + else + { + options.UseSqlServer(configuration.Get("Data:DefaultConnection:ConnectionString")); + } + }); // Add Identity services to the services container services.AddDefaultIdentity(configuration); + services.ConfigureFacebookAuthentication(options => + { + options.AppId = "[AppId]"; + options.AppSecret = "[AppSecret]"; + options.Notifications = new FacebookAuthenticationNotifications() + { + OnAuthenticated = FacebookNotifications.OnAuthenticated, + OnReturnEndpoint = FacebookNotifications.OnReturnEndpoint, + OnApplyRedirect = FacebookNotifications.OnApplyRedirect + }; + options.BackchannelHttpHandler = new FacebookMockBackChannelHttpHandler(); + options.StateDataFormat = new CustomStateDataFormat(); + options.Scope.Add("email"); + options.Scope.Add("read_friendlists"); + options.Scope.Add("user_checkins"); + }); + // Add MVC services to the services container services.AddMvc(); @@ -119,76 +134,56 @@ namespace MusicStore // Add cookie-based authentication to the request pipeline app.UseIdentity(); - var facebookOptions = new FacebookAuthenticationOptions() + app.UseFacebookAuthentication(); + + app.UseGoogleAuthentication(options => { - AppId = "[AppId]", - AppSecret = "[AppSecret]", - Notifications = new FacebookAuthenticationNotifications() - { - OnAuthenticated = FacebookNotifications.OnAuthenticated, - OnReturnEndpoint = FacebookNotifications.OnReturnEndpoint, - OnApplyRedirect = FacebookNotifications.OnApplyRedirect - }, - BackchannelHttpHandler = new FacebookMockBackChannelHttpHandler(), - StateDataFormat = new CustomStateDataFormat() - }; - - facebookOptions.Scope.Add("email"); - facebookOptions.Scope.Add("read_friendlists"); - facebookOptions.Scope.Add("user_checkins"); - - app.UseFacebookAuthentication(facebookOptions); - - app.UseGoogleAuthentication(new GoogleAuthenticationOptions() - { - ClientId = "[ClientId]", - ClientSecret = "[ClientSecret]", - AccessType = "offline", - Notifications = new GoogleAuthenticationNotifications() + options.ClientId = "[ClientId]"; + options.ClientSecret = "[ClientSecret]"; + options.AccessType = "offline"; + options.Notifications = new GoogleAuthenticationNotifications() { OnAuthenticated = GoogleNotifications.OnAuthenticated, OnReturnEndpoint = GoogleNotifications.OnReturnEndpoint, OnApplyRedirect = GoogleNotifications.OnApplyRedirect - }, - StateDataFormat = new CustomStateDataFormat(), - BackchannelHttpHandler = new GoogleMockBackChannelHttpHandler() + }; + options.StateDataFormat = new CustomStateDataFormat(); + options.BackchannelHttpHandler = new GoogleMockBackChannelHttpHandler(); }); - app.UseTwitterAuthentication(new TwitterAuthenticationOptions() + app.UseTwitterAuthentication(options => { - ConsumerKey = "[ConsumerKey]", - ConsumerSecret = "[ConsumerSecret]", - Notifications = new TwitterAuthenticationNotifications() + options.ConsumerKey = "[ConsumerKey]"; + options.ConsumerSecret = "[ConsumerSecret]"; + options.Notifications = new TwitterAuthenticationNotifications() { OnAuthenticated = TwitterNotifications.OnAuthenticated, OnReturnEndpoint = TwitterNotifications.OnReturnEndpoint, OnApplyRedirect = TwitterNotifications.OnApplyRedirect - }, - StateDataFormat = new CustomTwitterStateDataFormat(), - BackchannelHttpHandler = new TwitterMockBackChannelHttpHandler(), + }; + options.StateDataFormat = new CustomTwitterStateDataFormat(); + options.BackchannelHttpHandler = new TwitterMockBackChannelHttpHandler(); #if ASPNET50 - BackchannelCertificateValidator = null + options.BackchannelCertificateValidator = null; #endif }); - var microsoftAccountOptions = new MicrosoftAccountAuthenticationOptions() + app.UseMicrosoftAccountAuthentication(options => { - Caption = "MicrosoftAccount - Requires project changes", - ClientId = "[ClientId]", - ClientSecret = "[ClientSecret]", - Notifications = new MicrosoftAccountAuthenticationNotifications() + options.Caption = "MicrosoftAccount - Requires project changes"; + options.ClientId = "[ClientId]"; + options.ClientSecret = "[ClientSecret]"; + options.Notifications = new MicrosoftAccountAuthenticationNotifications() { OnAuthenticated = MicrosoftAccountNotifications.OnAuthenticated, OnReturnEndpoint = MicrosoftAccountNotifications.OnReturnEndpoint, OnApplyRedirect = MicrosoftAccountNotifications.OnApplyRedirect - }, - BackchannelHttpHandler = new MicrosoftAccountMockBackChannelHandler(), - StateDataFormat = new CustomStateDataFormat() - }; - - microsoftAccountOptions.Scope.Add("wl.basic"); - microsoftAccountOptions.Scope.Add("wl.signin"); - app.UseMicrosoftAccountAuthentication(microsoftAccountOptions); + }; + options.BackchannelHttpHandler = new MicrosoftAccountMockBackChannelHandler(); + options.StateDataFormat = new CustomStateDataFormat(); + options.Scope.Add("wl.basic"); + options.Scope.Add("wl.signin"); + }); // Add MVC to the request pipeline app.UseMvc(routes => diff --git a/src/MusicStore/Startup.cs b/src/MusicStore/Startup.cs index 860d528643..eb1b4b53ca 100644 --- a/src/MusicStore/Startup.cs +++ b/src/MusicStore/Startup.cs @@ -17,65 +17,98 @@ namespace MusicStore { public class Startup { - public void Configure(IApplicationBuilder app) + public Startup() { //Below code demonstrates usage of multiple configuration sources. For instance a setting say 'setting1' is found in both the registered sources, //then the later source will win. By this way a Local config can be overridden by a different setting while deployed remotely. - var configuration = new Configuration() + Configuration = new Configuration() .AddJsonFile("config.json") .AddEnvironmentVariables(); //All environment variables in the process's context flow in as configuration values. + } + public IConfiguration Configuration { get; private set; } + + public void ConfigureServices(IServiceCollection services) + { + //If this type is present - we're on mono + var runningOnMono = Type.GetType("Mono.Runtime") != null; + + // Add EF services to the services container + if (runningOnMono) + { + services.AddEntityFramework() + .AddInMemoryStore(); + } + else + { + services.AddEntityFramework() + .AddSqlServer(); + } + + services.AddScoped(); + + // Configure DbContext + services.ConfigureOptions(options => + { + options.DefaultAdminUserName = Configuration.Get("DefaultAdminUsername"); + options.DefaultAdminPassword = Configuration.Get("DefaultAdminPassword"); + if (runningOnMono) + { + options.UseInMemoryStore(); + } + else + { + options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString")); + } + }); + + // Add Identity services to the services container + services.AddDefaultIdentity(Configuration); + + services.ConfigureFacebookAuthentication(options => + { + options.AppId = "550624398330273"; + options.AppSecret = "10e56a291d6b618da61b1e0dae3a8954"; + }); + + services.ConfigureGoogleAuthentication(options => + { + options.ClientId = "977382855444.apps.googleusercontent.com"; + options.ClientSecret = "NafT482F70Vjj_9q1PU4B0pN"; + }); + + services.ConfigureTwitterAuthentication(options => + { + options.ConsumerKey = "9J3j3pSwgbWkgPFH7nAf0Spam"; + options.ConsumerSecret = "jUBYkQuBFyqp7G3CUB9SW3AfflFr9z3oQBiNvumYy87Al0W4h8"; + }); + + services.ConfigureMicrosoftAccountAuthentication(options => + { + options.Caption = "MicrosoftAccount - Requires project changes"; + options.ClientId = "000000004012C08A"; + options.ClientSecret = "GaMQ2hCnqAC6EcDLnXsAeBVIJOLmeutL"; + }); + + // Add MVC services to the services container + services.AddMvc(); + + //Add all SignalR related services to IoC. + services.AddSignalR(); + + //Add InMemoryCache + //Currently not able to AddSingleTon + services.AddInstance(new MemoryCache()); + } + + public void Configure(IApplicationBuilder app) + { //Error page middleware displays a nice formatted HTML page for any unhandled exceptions in the request pipeline. //Note: ErrorPageOptions.ShowAll to be used only at development time. Not recommended for production. app.UseErrorPage(ErrorPageOptions.ShowAll); - app.UseServices(services => - { - //If this type is present - we're on mono - var runningOnMono = Type.GetType("Mono.Runtime") != null; - - // Add EF services to the services container - if (runningOnMono) - { - services.AddEntityFramework() - .AddInMemoryStore(); - } - else - { - services.AddEntityFramework() - .AddSqlServer(); - } - - services.AddScoped(); - - // Configure DbContext - services.SetupOptions(options => - { - options.DefaultAdminUserName = configuration.Get("DefaultAdminUsername"); - options.DefaultAdminPassword = configuration.Get("DefaultAdminPassword"); - if (runningOnMono) - { - options.UseInMemoryStore(); - } - else - { - options.UseSqlServer(configuration.Get("Data:DefaultConnection:ConnectionString")); - } - }); - - // Add Identity services to the services container - services.AddDefaultIdentity(configuration); - - // Add MVC services to the services container - services.AddMvc(); - - //Add all SignalR related services to IoC. - services.AddSignalR(); - - //Add InMemoryCache - //Currently not able to AddSingleTon - services.AddInstance(new MemoryCache()); - }); + // Add services from ConfigureServices + app.UseServices(); //Configure SignalR app.UseSignalR(); @@ -86,23 +119,11 @@ namespace MusicStore // Add cookie-based authentication to the request pipeline app.UseIdentity(); - app.UseFacebookAuthentication(new FacebookAuthenticationOptions() - { - AppId = "550624398330273", - AppSecret = "10e56a291d6b618da61b1e0dae3a8954", - }); + app.UseFacebookAuthentication(); - app.UseGoogleAuthentication(new GoogleAuthenticationOptions() - { - ClientId = "977382855444.apps.googleusercontent.com", - ClientSecret = "NafT482F70Vjj_9q1PU4B0pN", - }); + app.UseGoogleAuthentication(); - app.UseTwitterAuthentication(new TwitterAuthenticationOptions() - { - ConsumerKey = "9J3j3pSwgbWkgPFH7nAf0Spam", - ConsumerSecret = "jUBYkQuBFyqp7G3CUB9SW3AfflFr9z3oQBiNvumYy87Al0W4h8", - }); + app.UseTwitterAuthentication(); //The MicrosoftAccount service has restrictions that prevent the use of http://localhost:5001/ for test applications. //As such, here is how to change this sample to uses http://ktesting.com:5001/ instead. @@ -119,12 +140,7 @@ namespace MusicStore //The sample app can then be run via: // k web - app.UseMicrosoftAccountAuthentication(new MicrosoftAccountAuthenticationOptions() - { - Caption = "MicrosoftAccount - Requires project changes", - ClientId = "000000004012C08A", - ClientSecret = "GaMQ2hCnqAC6EcDLnXsAeBVIJOLmeutL", - }); + app.UseMicrosoftAccountAuthentication(); // Add MVC to the request pipeline app.UseMvc(routes => diff --git a/src/MusicStore/StartupNtlmAuthentication.cs b/src/MusicStore/StartupNtlmAuthentication.cs index c28d70f93a..9c8820daf9 100644 --- a/src/MusicStore/StartupNtlmAuthentication.cs +++ b/src/MusicStore/StartupNtlmAuthentication.cs @@ -75,12 +75,12 @@ namespace MusicStore services.AddScoped(); // Configure DbContext - services.SetupOptions(options => - { - options.DefaultAdminUserName = configuration.Get("DefaultAdminUsername"); - options.DefaultAdminPassword = configuration.Get("DefaultAdminPassword"); - options.UseSqlServer(configuration.Get("Data:DefaultConnection:ConnectionString")); - }); + services.ConfigureOptions(options => + { + options.DefaultAdminUserName = configuration.Get("DefaultAdminUsername"); + options.DefaultAdminPassword = configuration.Get("DefaultAdminPassword"); + options.UseSqlServer(configuration.Get("Data:DefaultConnection:ConnectionString")); + }); // Add Identity services to the services container services.AddDefaultIdentity(configuration);