From 9127f5b930d92999cc350a9870ea4f1e04a571a0 Mon Sep 17 00:00:00 2001 From: Praburaj Date: Mon, 14 Apr 2014 12:35:56 -0700 Subject: [PATCH] A few changes with this checkin 1. Adding cookies middleware to the music store app 2. Enabling login, register & related management pages to render some UI for testing 3. Updated the LKG.json --- .../Controllers/AccountController.cs | 5 +- src/MusicStore/LKG.json | 47 ++++++++++--------- src/MusicStore/Logging/NullLogger.cs | 12 +++++ src/MusicStore/Logging/NullLoggerFactory.cs | 11 +++++ src/MusicStore/Startup.cs | 26 ++++++---- src/MusicStore/Views/Account/Login.cshtml | 26 +++++----- src/MusicStore/Views/Account/Manage.cshtml | 10 ++-- src/MusicStore/Views/Account/Register.cshtml | 13 +++-- .../Account/_ChangePasswordPartial.cshtml | 13 +++-- .../Views/Account/_SetPasswordPartial.cshtml | 8 ++-- src/MusicStore/project.json | 5 +- 11 files changed, 113 insertions(+), 63 deletions(-) create mode 100644 src/MusicStore/Logging/NullLogger.cs create mode 100644 src/MusicStore/Logging/NullLoggerFactory.cs diff --git a/src/MusicStore/Controllers/AccountController.cs b/src/MusicStore/Controllers/AccountController.cs index 756d9d33c9..94ef120cd7 100644 --- a/src/MusicStore/Controllers/AccountController.cs +++ b/src/MusicStore/Controllers/AccountController.cs @@ -65,6 +65,7 @@ namespace MusicStore.Controllers // // GET: /Account/Register [AllowAnonymous] + [HttpGet] //TODO: Do we need this. Without this I seem to be landing here irrespective of the HTTP verb? public IActionResult Register() { return View(); @@ -362,7 +363,7 @@ namespace MusicStore.Controllers private async Task SignIn(ApplicationUser user, bool isPersistent) { - this.Context.Response.SignOut(DefaultAuthenticationTypes.ExternalCookie); + //this.Context.Response.SignOut(DefaultAuthenticationTypes.ExternalCookie); var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); this.Context.Response.SignIn(identity, new AuthenticationProperties() { IsPersistent = isPersistent }); } @@ -447,7 +448,7 @@ namespace MusicStore.Controllers { public static string GetUserId(this IIdentity user) { - return string.Empty; + return user.Name; } public static Task GetExternalLoginInfo(this HttpResponse response) diff --git a/src/MusicStore/LKG.json b/src/MusicStore/LKG.json index 811498b480..ca5239b177 100644 --- a/src/MusicStore/LKG.json +++ b/src/MusicStore/LKG.json @@ -1,33 +1,36 @@ { "version": "0.1-alpha-*", "dependencies": { - "Helios": "0.1-alpha-243", - "Microsoft.AspNet.Abstractions": "0.1-alpha-239", - "Microsoft.AspNet.Mvc": "0.1-alpha-573", + "Helios": "0.1-alpha-254", + "Microsoft.AspNet.Abstractions": "0.1-alpha-249", + "Microsoft.AspNet.Mvc": "0.1-alpha-599", "Microsoft.AspNet.Razor": "0.1-alpha-181", "Microsoft.AspNet.ConfigurationModel": "0.1-alpha-174", - "Microsoft.AspNet.DependencyInjection": "0.1-alpha-243", - "Microsoft.AspNet.RequestContainer": "0.1-alpha-243", - "Microsoft.AspNet.Routing": "0.1-alpha-220", - "Microsoft.AspNet.Mvc.ModelBinding": "0.1-alpha-573", - "Microsoft.AspNet.Mvc.Core": "0.1-alpha-573", - "Microsoft.AspNet.Mvc.Razor": "0.1-alpha-573", - "Microsoft.AspNet.StaticFiles": "0.1-alpha-180", - "System.Security.Claims": "0.1-alpha-120", + "Microsoft.AspNet.DependencyInjection": "0.1-alpha-247", + "Microsoft.AspNet.RequestContainer": "0.1-alpha-250", + "Microsoft.AspNet.Routing": "0.1-alpha-228", + "Microsoft.AspNet.Mvc.ModelBinding": "0.1-alpha-599", + "Microsoft.AspNet.Mvc.Core": "0.1-alpha-599", + "Microsoft.AspNet.Mvc.Razor": "0.1-alpha-599", + "Microsoft.AspNet.StaticFiles": "0.1-alpha-188", + "System.Security.Claims": "0.1-alpha-124", "System.Security.Principal": "4.0.0.0", "Microsoft.AspNet.Security.DataProtection": "0.1-alpha-142", - "Microsoft.AspNet.Identity": "0.1-alpha-327", - "Microsoft.AspNet.Identity.Entity": "0.1-alpha-327", - "Microsoft.AspNet.Identity.InMemory": "0.1-alpha-327", - "Microsoft.Data.Entity": "0.1-alpha-419", - "Microsoft.Data.Relational": "0.1-alpha-419", + "Microsoft.AspNet.Identity": "0.1-alpha-347", + "Microsoft.AspNet.Identity.Entity": "0.1-alpha-347", + "Microsoft.AspNet.Identity.InMemory": "0.1-alpha-347", + "Microsoft.Data.Entity": "0.1-alpha-432", + "Microsoft.Data.Relational": "0.1-alpha-432", "Microsoft.Data.SqlServer": "0.1-pre-419", - "Microsoft.Data.InMemory": "0.1-alpha-419", - "Microsoft.Data.Migrations": "0.1-alpha-419", - "Microsoft.AspNet.Diagnostics": "0.1-alpha-124", - "Microsoft.AspNet.Hosting": "0.1-alpha-243", - "Microsoft.AspNet.Server.WebListener": "0.1-alpha-180", - "Microsoft.AspNet.Configuration.Json": "0.1-alpha-174" + "Microsoft.Data.InMemory": "0.1-alpha-432", + "Microsoft.Data.Migrations": "0.1-alpha-432", + "Microsoft.AspNet.Diagnostics": "0.1-alpha-133", + "Microsoft.AspNet.Hosting": "0.1-alpha-250", + "Microsoft.AspNet.Server.WebListener": "0.1-alpha-193", + "Microsoft.AspNet.Configuration.Json": "0.1-alpha-174", + "Microsoft.AspNet.Security": "0.1-alpha-102", + "Microsoft.AspNet.Security.Cookies": "0.1-alpha-102", + "Microsoft.AspNet.Logging": "0.1-alpha-159" }, "configurations": { "net45": { diff --git a/src/MusicStore/Logging/NullLogger.cs b/src/MusicStore/Logging/NullLogger.cs new file mode 100644 index 0000000000..f68f6647ea --- /dev/null +++ b/src/MusicStore/Logging/NullLogger.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNet.Logging; +using System; +namespace MusicStore.Logging +{ + public class NullLogger : ILogger + { + public bool WriteCore(TraceType eventType, int eventId, object state, Exception exception, Func formatter) + { + return false; + } + } +} \ No newline at end of file diff --git a/src/MusicStore/Logging/NullLoggerFactory.cs b/src/MusicStore/Logging/NullLoggerFactory.cs new file mode 100644 index 0000000000..1e3b184868 --- /dev/null +++ b/src/MusicStore/Logging/NullLoggerFactory.cs @@ -0,0 +1,11 @@ +using Microsoft.AspNet.Logging; +namespace MusicStore.Logging +{ + public class NullLoggerFactory : ILoggerFactory + { + public ILogger Create(string name) + { + return new NullLogger(); + } + } +} \ No newline at end of file diff --git a/src/MusicStore/Startup.cs b/src/MusicStore/Startup.cs index 5ee7dd7ac1..560b1188a6 100644 --- a/src/MusicStore/Startup.cs +++ b/src/MusicStore/Startup.cs @@ -1,23 +1,23 @@ using Microsoft.AspNet; using Microsoft.AspNet.Abstractions; -using Microsoft.AspNet.ConfigurationModel; using Microsoft.AspNet.Configuration.Json; +using Microsoft.AspNet.ConfigurationModel; using Microsoft.AspNet.DependencyInjection; using Microsoft.AspNet.DependencyInjection.Fallback; -using Microsoft.AspNet.DependencyInjection.NestedProviders; -using Microsoft.AspNet.RequestContainer; using Microsoft.AspNet.Diagnostics; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.InMemory; +using Microsoft.AspNet.Logging; using Microsoft.AspNet.Mvc; +using Microsoft.AspNet.RequestContainer; using Microsoft.AspNet.Routing; +using Microsoft.AspNet.Security.Cookies; +using Microsoft.Net.Runtime; +using MusicStore.Logging; using MusicStore.Models; using MusicStore.Web.Models; using System; -using System.Collections.Generic; using System.IO; -using System.Reflection; -using Microsoft.Net.Runtime; public class Startup { @@ -28,14 +28,22 @@ public class Startup //ErrorPageOptions.ShowAll to be used only at development time. Not recommended for production. app.UseErrorPage(ErrorPageOptions.ShowAll); + var serviceCollection = new ServiceCollection(); + serviceCollection.AddInstance(new NullLoggerFactory()); + serviceCollection.Add(MvcServices.GetDefaultServices()); + app.UseContainer(serviceCollection.BuildServiceProvider(app.ServiceProvider)); + app.UseFileServer(); - var serviceProvider = MvcServices.GetDefaultServices().BuildServiceProvider(app.ServiceProvider); - app.UseContainer(serviceProvider); + app.UseCookieAuthentication(new CookieAuthenticationOptions() + { + AuthenticationType = "Application", + LoginPath = new PathString("/Account/Login") + }); var routes = new RouteCollection() { - DefaultHandler = new MvcApplication(serviceProvider), + DefaultHandler = new MvcApplication(app.ServiceProvider), }; routes.MapRoute( diff --git a/src/MusicStore/Views/Account/Login.cshtml b/src/MusicStore/Views/Account/Login.cshtml index 5100f8b43e..e1c0331315 100644 --- a/src/MusicStore/Views/Account/Login.cshtml +++ b/src/MusicStore/Views/Account/Login.cshtml @@ -12,29 +12,32 @@
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" })) { - @Html.AntiForgeryToken() + @*@Html.AntiForgeryToken()*@

Use a local account to log in.


@Html.ValidationSummary(true)
- @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" }) + @*@Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })*@ +
@Html.TextBoxFor(m => m.UserName, new { @class = "form-control" }) - @Html.ValidationMessageFor(m => m.UserName) + @*@Html.ValidationMessageFor(m => m.UserName)*@
- @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" }) + @*@Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })*@ +
@Html.PasswordFor(m => m.Password, new { @class = "form-control" }) - @Html.ValidationMessageFor(m => m.Password) + @*@Html.ValidationMessageFor(m => m.Password)*@
@Html.CheckBoxFor(m => m.RememberMe) - @Html.LabelFor(m => m.RememberMe) + @*@Html.LabelFor(m => m.RememberMe)*@ +
@@ -49,11 +52,12 @@ }
-
-
- @Html.Partial("_ExternalLoginsListPartial", new { Action = "ExternalLogin", ReturnUrl = ViewBag.ReturnUrl }) -
-
+ + @*
+
+ @Html.Partial("_ExternalLoginsListPartial", new { Action = "ExternalLogin", ReturnUrl = ViewBag.ReturnUrl }) +
+
*@ @section Scripts { @*Bug : Script helpers are out of scope for alpha*@ diff --git a/src/MusicStore/Views/Account/Manage.cshtml b/src/MusicStore/Views/Account/Manage.cshtml index c69125429f..65b2e52dff 100644 --- a/src/MusicStore/Views/Account/Manage.cshtml +++ b/src/MusicStore/Views/Account/Manage.cshtml @@ -7,23 +7,23 @@ }

@ViewBag.Title.

-

@ViewBag.StatusMessage

+
@if (ViewBag.HasLocalPassword) { - @Html.Partial("_ChangePasswordPartial") + await Html.PartialAsync("_ChangePasswordPartial"); } else { - @Html.Partial("_SetPasswordPartial") + await Html.PartialAsync("_SetPasswordPartial"); } -
+ @*
@Html.Action("RemoveAccountList") @Html.Partial("_ExternalLoginsListPartial", new { Action = "LinkLogin", ReturnUrl = ViewBag.ReturnUrl }) -
+
*@
diff --git a/src/MusicStore/Views/Account/Register.cshtml b/src/MusicStore/Views/Account/Register.cshtml index f6c2107aaf..244f2b6cb7 100644 --- a/src/MusicStore/Views/Account/Register.cshtml +++ b/src/MusicStore/Views/Account/Register.cshtml @@ -9,26 +9,29 @@ @using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) { - @Html.AntiForgeryToken() + @*@Html.AntiForgeryToken()*@

Create a new account.


@Html.ValidationSummary()
- @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" }) + @*@Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })*@ +
@Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
- @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" }) + @*@Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })*@ +
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
- @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" }) + @*@Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })*@ +
- @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" }) + @*@Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })*@
diff --git a/src/MusicStore/Views/Account/_ChangePasswordPartial.cshtml b/src/MusicStore/Views/Account/_ChangePasswordPartial.cshtml index 2363c3340b..c48462a6ef 100644 --- a/src/MusicStore/Views/Account/_ChangePasswordPartial.cshtml +++ b/src/MusicStore/Views/Account/_ChangePasswordPartial.cshtml @@ -1,28 +1,31 @@ @using Microsoft.AspNet.Identity @model MusicStore.Models.ManageUserViewModel -

You're logged in as @User.Identity.GetUserName().

+

You're logged in as @Context.HttpContext.User.Identity.Name.

@using (Html.BeginForm("Manage", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) { - @Html.AntiForgeryToken() + @*@Html.AntiForgeryToken()*@

Change Password Form


@Html.ValidationSummary()
- @Html.LabelFor(m => m.OldPassword, new { @class = "col-md-2 control-label" }) + @*@Html.LabelFor(m => m.OldPassword, new { @class = "col-md-2 control-label" })*@ +
@Html.PasswordFor(m => m.OldPassword, new { @class = "form-control" })
- @Html.LabelFor(m => m.NewPassword, new { @class = "col-md-2 control-label" }) + @*@Html.LabelFor(m => m.NewPassword, new { @class = "col-md-2 control-label" })*@ +
@Html.PasswordFor(m => m.NewPassword, new { @class = "form-control" })
- @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" }) + @*@Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })*@ +
@Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
diff --git a/src/MusicStore/Views/Account/_SetPasswordPartial.cshtml b/src/MusicStore/Views/Account/_SetPasswordPartial.cshtml index ec0173df64..ae749a0f7d 100644 --- a/src/MusicStore/Views/Account/_SetPasswordPartial.cshtml +++ b/src/MusicStore/Views/Account/_SetPasswordPartial.cshtml @@ -7,19 +7,21 @@ @using (Html.BeginForm("Manage", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) { - @Html.AntiForgeryToken() + @*@Html.AntiForgeryToken()*@

Create Local Login


@Html.ValidationSummary()
- @Html.LabelFor(m => m.NewPassword, new { @class = "col-md-2 control-label" }) + @*@Html.LabelFor(m => m.NewPassword, new { @class = "col-md-2 control-label" })*@ +
@Html.PasswordFor(m => m.NewPassword, new { @class = "form-control" })
- @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" }) + @*@Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })*@ +
@Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
diff --git a/src/MusicStore/project.json b/src/MusicStore/project.json index a62283c080..11391916f2 100644 --- a/src/MusicStore/project.json +++ b/src/MusicStore/project.json @@ -27,7 +27,10 @@ "Microsoft.AspNet.Diagnostics": "0.1-alpha-*", "Microsoft.AspNet.Hosting": "0.1-alpha-*", "Microsoft.AspNet.Server.WebListener": "0.1-alpha-*", - "Microsoft.AspNet.Configuration.Json": "0.1-alpha-*" + "Microsoft.AspNet.Configuration.Json": "0.1-alpha-*", + "Microsoft.AspNet.Security": "0.1-alpha-*", + "Microsoft.AspNet.Security.Cookies": "0.1-alpha-*", + "Microsoft.AspNet.Logging": "0.1-alpha-*" }, "commands": { "web": "Microsoft.AspNet.Hosting server.name=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:5001"