From 6cf7cbfa5bc98347391b4cc9cfb85021b89a820c Mon Sep 17 00:00:00 2001 From: Praburaj Date: Wed, 3 Sep 2014 16:57:38 -0700 Subject: [PATCH] Adding all the Social login middlewares to the pipeline Still some of the identity helpers have not lighted up. So it will not work fully. --- .../Controllers/AccountController.cs | 2 +- src/MusicStore/Startup.cs | 40 +++++++++++++++++++ src/MusicStore/Views/Account/Login.cshtml | 10 ++--- .../Account/_ExternalLoginsListPartial.cshtml | 13 ++++-- src/MusicStore/project.json | 4 ++ 5 files changed, 59 insertions(+), 10 deletions(-) diff --git a/src/MusicStore/Controllers/AccountController.cs b/src/MusicStore/Controllers/AccountController.cs index 015fec7626..9e3443a3f3 100644 --- a/src/MusicStore/Controllers/AccountController.cs +++ b/src/MusicStore/Controllers/AccountController.cs @@ -270,7 +270,7 @@ namespace MusicStore.Controllers [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] - public ActionResult ExternalLogin(string provider, string returnUrl) + public ActionResult ExternalLogin(string provider, string returnUrl = null) { // Request a redirect to the external login provider var redirectUri = Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }); diff --git a/src/MusicStore/Startup.cs b/src/MusicStore/Startup.cs index 8f670f11dc..64b178ac70 100644 --- a/src/MusicStore/Startup.cs +++ b/src/MusicStore/Startup.cs @@ -9,6 +9,11 @@ using Microsoft.Data.Entity; using Microsoft.Framework.ConfigurationModel; using Microsoft.Framework.DependencyInjection; using MusicStore.Models; +using Microsoft.AspNet.Security.Facebook; +using Microsoft.AspNet.Security.Google; +using Microsoft.AspNet.Security.Twitter; +using Microsoft.AspNet.Security.MicrosoftAccount; +using Microsoft.AspNet.Security; namespace MusicStore { @@ -26,6 +31,8 @@ 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 @@ -77,6 +84,14 @@ namespace MusicStore // Add static files to the request pipeline app.UseStaticFiles(); + // Add cookie-based authentication to the request pipeline + app.UseCookieAuthentication(new CookieAuthenticationOptions + { + AuthenticationType = "External", + AuthenticationMode = AuthenticationMode.Passive, + ExpireTimeSpan = TimeSpan.FromMinutes(5) + }); + // Add cookie-based authentication to the request pipeline app.UseCookieAuthentication(new CookieAuthenticationOptions { @@ -86,6 +101,31 @@ namespace MusicStore app.UseTwoFactorSignInCookies(); + app.UseFacebookAuthentication(new FacebookAuthenticationOptions() + { + AppId = "[AppId]", + AppSecret = "[AppSecret]", + }); + + app.UseGoogleAuthentication(new GoogleAuthenticationOptions() + { + ClientId = "[ClientId]", + ClientSecret = "[ClientSecret]", + }); + + app.UseTwitterAuthentication(new TwitterAuthenticationOptions() + { + ConsumerKey = "[ConsumerKey]", + ConsumerSecret = "[ConsumerSecret]", + }); + + app.UseMicrosoftAccountAuthentication(new MicrosoftAccountAuthenticationOptions() + { + Caption = "MicrosoftAccount - Requires project changes", + ClientId = "[ClientId]", + ClientSecret = "[ClientSecret]", + }); + // Add MVC to the request pipeline app.UseMvc(routes => { diff --git a/src/MusicStore/Views/Account/Login.cshtml b/src/MusicStore/Views/Account/Login.cshtml index 0b8cd41a86..27a628c27c 100644 --- a/src/MusicStore/Views/Account/Login.cshtml +++ b/src/MusicStore/Views/Account/Login.cshtml @@ -52,11 +52,11 @@ } - @*
-
- @Html.PartialAsync("_ExternalLoginsListPartial", new ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl }) -
-
*@ +
+
+ @await Html.PartialAsync("_ExternalLoginsListPartial", new ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl }) +
+
@section Scripts { @*TODO : Until script helpers are available, adding script references manually*@ diff --git a/src/MusicStore/Views/Account/_ExternalLoginsListPartial.cshtml b/src/MusicStore/Views/Account/_ExternalLoginsListPartial.cshtml index b47662f1c9..3a2ad1d25b 100644 --- a/src/MusicStore/Views/Account/_ExternalLoginsListPartial.cshtml +++ b/src/MusicStore/Views/Account/_ExternalLoginsListPartial.cshtml @@ -5,7 +5,8 @@
@{ var loginProviders = Context.GetAuthenticationTypes(); - if (loginProviders.Count() == 0) { + if (loginProviders.Count() == 0) + {

There are no external authentication services configured. See this article @@ -13,12 +14,16 @@

} - else { - using (Html.BeginForm("ExternalLogin", "Account", new { ReturnUrl = Model.ReturnUrl })) { + else + { +

asdfasdf

+ using (Html.BeginForm("ExternalLogin", "Account", new { ReturnUrl = Model.ReturnUrl })) + { @Html.AntiForgeryToken()

- @foreach (AuthenticationDescription p in loginProviders) { + @foreach (AuthenticationDescription p in loginProviders) + { }

diff --git a/src/MusicStore/project.json b/src/MusicStore/project.json index f747f1253e..492274c298 100644 --- a/src/MusicStore/project.json +++ b/src/MusicStore/project.json @@ -14,6 +14,10 @@ "Microsoft.AspNet.Identity.SqlServer": "3.0.0-*", "Microsoft.AspNet.Identity.Authentication": "3.0.0-*", "Microsoft.AspNet.Security.Cookies": "1.0.0-*", + "Microsoft.AspNet.Security.Facebook": "1.0.0-*", + "Microsoft.AspNet.Security.Google": "1.0.0-*", + "Microsoft.AspNet.Security.MicrosoftAccount": "1.0.0-*", + "Microsoft.AspNet.Security.Twitter": "1.0.0-*", "Microsoft.AspNet.StaticFiles": "1.0.0-*", "EntityFramework.SqlServer": "7.0.0-*", /*For Mono*/