Using session to store the cart details instead of cookies.

This commit is contained in:
Praburaj 2015-01-20 17:28:38 -08:00
parent 6b0c2bb01f
commit d29f24f4ad
9 changed files with 44 additions and 15 deletions

View File

@ -1,8 +1,8 @@
REM copy the AspNet.Loader.dll to bin folder REM copy the AspNet.Loader.dll to bin folder
md bin md wwwroot\bin
SET ASPNETLOADER_PACKAGE_BASEPATH=%USERPROFILE%\.dotnet\packages\Microsoft.AspNet.Loader.IIS.Interop SET ASPNETLOADER_PACKAGE_BASEPATH=%USERPROFILE%\.dotnet\packages\Microsoft.AspNet.Loader.IIS.Interop
REM figure out the path of AspNet.Loader.dll REM figure out the path of AspNet.Loader.dll
FOR /F %%j IN ('dir /b /o:-d %ASPNETLOADER_PACKAGE_BASEPATH%\*') do (SET AspNetLoaderPath=%ASPNETLOADER_PACKAGE_BASEPATH%\%%j\tools\AspNet.Loader.dll) FOR /F %%j IN ('dir /b /o:-d %ASPNETLOADER_PACKAGE_BASEPATH%\*') do (SET AspNetLoaderPath=%ASPNETLOADER_PACKAGE_BASEPATH%\%%j\tools\AspNet.Loader.dll)
echo Found AspNetLoader.dll at %AspNetLoaderPath%. Copying to bin\ echo Found AspNetLoader.dll at %AspNetLoaderPath%. Copying to bin\
copy %AspNetLoaderPath% bin\ copy %AspNetLoaderPath% wwwroot\bin\

View File

@ -3,4 +3,4 @@ SET KRE_HOME=%USERPROFILE%\.kre\
REM copy the AspNet.Loader.dll to the bin folder REM copy the AspNet.Loader.dll to the bin folder
call CopyAspNetLoader.cmd call CopyAspNetLoader.cmd
"%ProgramFiles(x86)%\iis Express\iisexpress.exe" /port:5001 /path:"%cd%" "%ProgramFiles(x86)%\iis Express\iisexpress.exe" /port:5001 /path:"%cd%\wwwroot"

View File

@ -147,22 +147,17 @@ namespace MusicStore.Models
} }
// We're using HttpContextBase to allow access to cookies. // We're using HttpContextBase to allow access to cookies.
public string GetCartId(HttpContext context) private string GetCartId(HttpContext context)
{ {
var sessionCookie = context.Request.Cookies.Get("Session"); var cartId = context.Session.GetString("Session");
string cartId = null;
if (string.IsNullOrWhiteSpace(sessionCookie)) if (cartId == null)
{ {
//A GUID to hold the cartId. //A GUID to hold the cartId.
cartId = Guid.NewGuid().ToString(); cartId = Guid.NewGuid().ToString();
// Send cart Id as a cookie to the client. // Send cart Id as a cookie to the client.
context.Response.Cookies.Append("Session", cartId); context.Session.SetString("Session", cartId);
}
else
{
cartId = sessionCookie;
} }
return cartId; return cartId;

View File

@ -87,6 +87,10 @@ namespace MusicStore
//Add InMemoryCache //Add InMemoryCache
services.AddSingleton<IMemoryCache, MemoryCache>(); services.AddSingleton<IMemoryCache, MemoryCache>();
// Add session related services.
services.AddCachingServices();
services.AddSessionServices();
// Configure Auth // Configure Auth
services.Configure<AuthorizationOptions>(options => services.Configure<AuthorizationOptions>(options =>
{ {
@ -138,6 +142,9 @@ namespace MusicStore
public void Configure(IApplicationBuilder app) public void Configure(IApplicationBuilder app)
{ {
// Configure Session.
app.UseSession();
//Configure SignalR //Configure SignalR
app.UseSignalR(); app.UseSignalR();

View File

@ -90,6 +90,10 @@ namespace MusicStore
//Add InMemoryCache //Add InMemoryCache
services.AddSingleton<IMemoryCache, MemoryCache>(); services.AddSingleton<IMemoryCache, MemoryCache>();
// Add session related services.
services.AddCachingServices();
services.AddSessionServices();
// Configure Auth // Configure Auth
services.Configure<AuthorizationOptions>(options => services.Configure<AuthorizationOptions>(options =>
{ {
@ -97,6 +101,9 @@ namespace MusicStore
}); });
}); });
// Configure Session.
app.UseSession();
//Configure SignalR //Configure SignalR
app.UseSignalR(); app.UseSignalR();

View File

@ -75,6 +75,10 @@ namespace MusicStore
//Add InMemoryCache //Add InMemoryCache
services.AddSingleton<IMemoryCache, MemoryCache>(); services.AddSingleton<IMemoryCache, MemoryCache>();
// Add session related services.
services.AddCachingServices();
services.AddSessionServices();
// Configure Auth // Configure Auth
services.Configure<AuthorizationOptions>(options => services.Configure<AuthorizationOptions>(options =>
{ {
@ -104,6 +108,9 @@ namespace MusicStore
public void Configure(IApplicationBuilder app) public void Configure(IApplicationBuilder app)
{ {
// Configure Session.
app.UseSession();
//Configure SignalR //Configure SignalR
app.UseSignalR(); app.UseSignalR();

View File

@ -27,9 +27,10 @@
"Microsoft.AspNet.Security.Twitter": "1.0.0-*", "Microsoft.AspNet.Security.Twitter": "1.0.0-*",
"Microsoft.AspNet.Server.IIS": "1.0.0-*", "Microsoft.AspNet.Server.IIS": "1.0.0-*",
"Microsoft.AspNet.Server.WebListener": "1.0.0-*", "Microsoft.AspNet.Server.WebListener": "1.0.0-*",
"Microsoft.AspNet.Session": "1.0.0-*",
"Microsoft.AspNet.SignalR.Server": "3.0.0-*", "Microsoft.AspNet.SignalR.Server": "3.0.0-*",
"Microsoft.AspNet.StaticFiles": "1.0.0-*", "Microsoft.AspNet.StaticFiles": "1.0.0-*",
"Microsoft.Framework.Cache.Memory": "1.0.0-*", "Microsoft.Framework.Cache.Distributed": "1.0.0-*", // For Session.
"Microsoft.Framework.CodeGenerators.Mvc": "1.0.0-*", "Microsoft.Framework.CodeGenerators.Mvc": "1.0.0-*",
"Microsoft.Framework.ConfigurationModel.Json": "1.0.0-*", "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-*",
"Microsoft.Framework.Logging.Console": "1.0.0-*" "Microsoft.Framework.Logging.Console": "1.0.0-*"

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
@ -14,4 +14,9 @@
<SchemaVersion>2.0</SchemaVersion> <SchemaVersion>2.0</SchemaVersion>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" /> <Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project> <ProjectExtensions>
<VisualStudio>
<UserProperties project_1json__JSONSchema="http://www.asp.net/media/4878834/project.json" />
</VisualStudio>
</ProjectExtensions>
</Project>

View File

@ -111,6 +111,10 @@ namespace MusicStore
//Add InMemoryCache //Add InMemoryCache
services.AddSingleton<IMemoryCache, MemoryCache>(); services.AddSingleton<IMemoryCache, MemoryCache>();
// Add session related services.
services.AddCachingServices();
services.AddSessionServices();
// Configure Auth // Configure Auth
services.Configure<AuthorizationOptions>(options => services.Configure<AuthorizationOptions>(options =>
{ {
@ -118,6 +122,9 @@ namespace MusicStore
}); });
}); });
// Configure Session.
app.UseSession();
//To gracefully shutdown the server - Not for production scenarios //To gracefully shutdown the server - Not for production scenarios
app.Map("/shutdown", shutdown => app.Map("/shutdown", shutdown =>
{ {