diff --git a/netcore/AspNetCore GeodeSession Sample/AspNetCore GeodeSession Sample.csproj b/netcore/AspNetCore GeodeSession Sample/AspNetCore GeodeSession Sample.csproj new file mode 100644 index 0000000000..fb587cba84 --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/AspNetCore GeodeSession Sample.csproj @@ -0,0 +1,13 @@ + + + + netcoreapp3.1 + x64 + + + + + + + + diff --git a/netcore/AspNetCore GeodeSession Sample/AspNetCore GeodeSession Sample.csproj.user b/netcore/AspNetCore GeodeSession Sample/AspNetCore GeodeSession Sample.csproj.user new file mode 100644 index 0000000000..9dd7dfaa1b --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/AspNetCore GeodeSession Sample.csproj.user @@ -0,0 +1,6 @@ + + + + IIS Express + + diff --git a/netcore/AspNetCore GeodeSession Sample/Extensions/SessionExtensions.cs b/netcore/AspNetCore GeodeSession Sample/Extensions/SessionExtensions.cs new file mode 100644 index 0000000000..367ee56664 --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/Extensions/SessionExtensions.cs @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System.Text.Json; +using Microsoft.AspNetCore.Http; + +namespace Web.Extensions { +#region snippet1 + public static class SessionExtensions { + public static void Set(this ISession session, string key, T value) { + session.SetString(key, JsonSerializer.Serialize(value)); + } + + public static T Get(this ISession session, string key) { + var value = session.GetString(key); + return value == null ? default : JsonSerializer.Deserialize(value); + } + } +#endregion +} + +namespace Web.Extensions2 { + // Alternate approach + + public static class SessionExtensions { + public static void Set(this ISession session, string key, T value) { + session.SetString(key, JsonSerializer.Serialize(value)); + } + + public static bool TryGet(this ISession session, string key, out T value) { + var state = session.GetString(key); + value = default; + if (state == null) { + return false; + } + value = JsonSerializer.Deserialize(state); + return true; + } + } +} diff --git a/netcore/AspNetCore GeodeSession Sample/Middleware/HttpContextItemsMiddleware.cs b/netcore/AspNetCore GeodeSession Sample/Middleware/HttpContextItemsMiddleware.cs new file mode 100644 index 0000000000..8b7ca4bcda --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/Middleware/HttpContextItemsMiddleware.cs @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; + +namespace SessionSample.Middleware { +#region snippet1 + public class HttpContextItemsMiddleware { + private readonly RequestDelegate _next; + public static readonly object HttpContextItemsMiddlewareKey = new Object(); + + public HttpContextItemsMiddleware(RequestDelegate next) { + _next = next; + } + + public async Task Invoke(HttpContext httpContext) { + httpContext.Items[HttpContextItemsMiddlewareKey] = "K-9"; + + await _next(httpContext); + } + } + + public static class HttpContextItemsMiddlewareExtensions { + public static IApplicationBuilder UseHttpContextItemsMiddleware(this IApplicationBuilder app) { + return app.UseMiddleware(); + } + } +#endregion +} diff --git a/netcore/AspNetCore GeodeSession Sample/Models/BasicAuthInitialize.cs b/netcore/AspNetCore GeodeSession Sample/Models/BasicAuthInitialize.cs new file mode 100644 index 0000000000..067d41d74a --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/Models/BasicAuthInitialize.cs @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using Apache.Geode.Client; +using System; +using System.Collections.Generic; + +namespace GemFireSessionState.Models +{ + public class BasicAuthInitialize : IAuthInitialize + { + private string _username; + private string _password; + + public BasicAuthInitialize(string username, string password) + { + _username = username; + _password = password; + } + + public void Close() + { + } + + public Dictionary GetCredentials() + { + Console.WriteLine("SimpleAuthInitialize::GetCredentials called"); + var credentials = new Dictionary(); + credentials.Add("security-username", "root"); + credentials.Add("security-password", "root-password"); + return credentials; + } + } +} diff --git a/netcore/AspNetCore GeodeSession Sample/Models/ErrorViewModel.cs b/netcore/AspNetCore GeodeSession Sample/Models/ErrorViewModel.cs new file mode 100644 index 0000000000..49f70efc06 --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/Models/ErrorViewModel.cs @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace SessionSample.Models +{ + public class ErrorViewModel + { + } +} diff --git a/netcore/AspNetCore GeodeSession Sample/Pages/Error.cshtml b/netcore/AspNetCore GeodeSession Sample/Pages/Error.cshtml new file mode 100644 index 0000000000..7e0919b3c1 --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/Pages/Error.cshtml @@ -0,0 +1,42 @@ +@* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *@ +@page +@model ErrorModel +@{ + ViewData["Title"] = "Error"; +} + +

Error.

+

An error occurred while processing your request.

+ +@if (Model.ShowRequestId) +{ +

+ Request ID: @Model.RequestId +

+} + +

Development Mode

+

+ Swapping to the Development environment displays detailed information about the error that occurred. +

+

+ The Development environment shouldn't be enabled for deployed applications. + It can result in displaying sensitive information from exceptions to end users. + For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development + and restarting the app. +

diff --git a/netcore/AspNetCore GeodeSession Sample/Pages/Error.cshtml.cs b/netcore/AspNetCore GeodeSession Sample/Pages/Error.cshtml.cs new file mode 100644 index 0000000000..b6245b6337 --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/Pages/Error.cshtml.cs @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; + +namespace SessionSample.Pages { + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public class ErrorModel : PageModel { + public string RequestId { get; set; } + + public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); + + public void OnGet() { + RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; + } + } +} diff --git a/netcore/AspNetCore GeodeSession Sample/Pages/Index.cshtml b/netcore/AspNetCore GeodeSession Sample/Pages/Index.cshtml new file mode 100644 index 0000000000..47bcf75bb8 --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/Pages/Index.cshtml @@ -0,0 +1,99 @@ +@* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*@ +@page +@using Microsoft.AspNetCore.Http +@model IndexModel +@{ + ViewData["Title"] = "Asp.Net Core Session Sample"; +} + +

@ViewData["Title"]

+ +

State management

+ +
+
+
+
+
+ +

Name and Age

+
+
+

+ The name and age are stored in session. Select the Change Age + button to update the session to a new random age value. +

+

+ Session values by the model with + @@Model.<PropertyName>: +

+

Name: @Model.SessionInfo_Name Age: @Model.SessionInfo_Age

+
+

Session values direct

+

+ Name: @HttpContext.Session.GetString(IndexModel.SessionKeyName) + Age: + @HttpContext.Session.GetInt32(IndexModel.SessionKeyAge).ToString() +

+
+
+
+
+
+ +
+
+
+
+

HttpContext.Items Middleware Value

+
+
+

+ The middleware value is set into the HttpContext.Items collection by + the HttpContextItemsMiddleware before Razor Pages processes the request. + The value is retreived by the page and displayed. +

+

Value: @Model.SessionInfo_MiddlewareValue

+
+
+
+
+ +
+
+
+
+
+ + Reload Page (No Update) +

Session Time

+
+
+

+ The session time is stored in session. Select the + Reload Page (No Update) + button to display the current time and the time stored in session. + Select the Update Session Time button to store the current time in session. +

+

Current Time: @Model.SessionInfo_CurrentTime

+

Session Time: @Model.SessionInfo_SessionTime

+
+
+
+
+
diff --git a/netcore/AspNetCore GeodeSession Sample/Pages/Index.cshtml.cs b/netcore/AspNetCore GeodeSession Sample/Pages/Index.cshtml.cs new file mode 100644 index 0000000000..723979e8b6 --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/Pages/Index.cshtml.cs @@ -0,0 +1,62 @@ +using System; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; +using SessionSample.Middleware; +using Web.Extensions; + +namespace SessionSample.Pages { + public class IndexModel : PageModel { + public const string SessionKeyName = "_Name"; + public const string SessionKeyAge = "_Age"; + const string SessionKeyTime = "_Time"; + + public string SessionInfo_Name { get; private set; } + public string SessionInfo_Age { get; private set; } + public string SessionInfo_CurrentTime { get; private set; } + public string SessionInfo_SessionTime { get; private set; } + public string SessionInfo_MiddlewareValue { get; private set; } + + public void OnGet() { + // Requires: using Microsoft.AspNetCore.Http; + if (string.IsNullOrEmpty(HttpContext.Session.GetString(SessionKeyName))) { + HttpContext.Session.SetString(SessionKeyName, "The Doctor"); + HttpContext.Session.SetInt32(SessionKeyAge, 35); + } + + var name = HttpContext.Session.GetString(SessionKeyName); + var age = HttpContext.Session.GetInt32(SessionKeyAge); + SessionInfo_Name = name; + SessionInfo_Age = age.ToString(); + + var currentTime = DateTime.Now; + + // Requires SessionExtensions from sample download. + if (HttpContext.Session.Get(SessionKeyTime) == default) { + HttpContext.Session.Set(SessionKeyTime, currentTime); + } + + SessionInfo_CurrentTime = currentTime.ToString("H:mm:ss tt"); + SessionInfo_SessionTime = + HttpContext.Session.Get(SessionKeyTime).ToString("H:mm:ss tt"); + + HttpContext.Items.TryGetValue(HttpContextItemsMiddleware.HttpContextItemsMiddlewareKey, + out var middlewareSetValue); + SessionInfo_MiddlewareValue = middlewareSetValue?.ToString() ?? "Middleware value not set!"; + } + + public IActionResult OnPostUpdateSessionDate() { + HttpContext.Session.Set(SessionKeyTime, DateTime.Now); + + return RedirectToPage(); + } + + public IActionResult OnPostChangeAge() { + var r = new Random(); + + HttpContext.Session.SetInt32(SessionKeyAge, r.Next(35, 65)); + + return RedirectToPage(); + } + } +} diff --git a/netcore/AspNetCore GeodeSession Sample/Pages/Shared/_Layout.cshtml b/netcore/AspNetCore GeodeSession Sample/Pages/Shared/_Layout.cshtml new file mode 100644 index 0000000000..4465c65d58 --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/Pages/Shared/_Layout.cshtml @@ -0,0 +1,237 @@ +@* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*@ + + + + + + @ViewData["Title"] + + + +
+ @RenderBody() +
+ + diff --git a/netcore/AspNetCore GeodeSession Sample/Pages/_ViewImports.cshtml b/netcore/AspNetCore GeodeSession Sample/Pages/_ViewImports.cshtml new file mode 100644 index 0000000000..7bf76d46e4 --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/Pages/_ViewImports.cshtml @@ -0,0 +1,19 @@ +@* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*@ +@using SessionSample +@namespace SessionSample.Pages +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/netcore/AspNetCore GeodeSession Sample/Pages/_ViewStart.cshtml b/netcore/AspNetCore GeodeSession Sample/Pages/_ViewStart.cshtml new file mode 100644 index 0000000000..7e03e6df38 --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/Pages/_ViewStart.cshtml @@ -0,0 +1,19 @@ +@* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. +*@ +@{ + Layout = "_Layout"; +} diff --git a/netcore/AspNetCore GeodeSession Sample/Program.cs b/netcore/AspNetCore GeodeSession Sample/Program.cs new file mode 100644 index 0000000000..4495aabe51 --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/Program.cs @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; + +namespace SessionSample { + public class Program { + public static void Main(string[] args) { + CreateWebHostBuilder(args).Build().Run(); + } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + WebHost.CreateDefaultBuilder(args).UseStartup(); + } +} diff --git a/netcore/AspNetCore GeodeSession Sample/Properties/launchSettings.json b/netcore/AspNetCore GeodeSession Sample/Properties/launchSettings.json new file mode 100644 index 0000000000..5fdcaca833 --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/Properties/launchSettings.json @@ -0,0 +1,28 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:50795/", + "sslPort": 44315 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "nativeDebugging": true + }, + "SessionSample": { + "commandName": "Project", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "https://localhost:5001;http://localhost:5000" + } + } +} diff --git a/netcore/AspNetCore GeodeSession Sample/README.md b/netcore/AspNetCore GeodeSession Sample/README.md new file mode 100644 index 0000000000..38b0a9b5a0 --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/README.md @@ -0,0 +1,58 @@ +You can configure GeodeDistributedCache as a distributed cache for your ASP.NET Core application using either of the options described below. + +--- +NetCore-Session provides the AddGeodeDistributedCache() extension method on IServiceCollection, which requires just an Apache Geode region name and any optional configurations to store the sessions. + +There are two methods to specify configurations: +1. Through your application in Startup.cs or +2. In JSON format in Appsettings.json of your application. + +## Method 1: Specifying Configurations In Startup.cs + +The AddGeodeSessionStateCache() method is an extension of the AddDistributedCache() method provided by ASP.NET Core. This method takes configuration settings in Startup.cs of your application, or reads them from the specified JSON file. + +Add the following method and options in Startup.cs of your application: + +```c# +public void ConfigureServices(IServiceCollection services) +{ + //Add framework services + services.AddMvc(); + + services.AddGeodeSessionStateCache(configuration => + { + configuration.RegionName = "geodeSessionState"; + }); +} +``` + +## Method 2: Specifying Configurations In appsettings.json +You can also specify the configuration in JSON format using the appsettings.json file for your application. Using this method, you can refer to the configurations by providing the name of the section containing JSON format configurations in Startup.cs. + +appsettings.json: + +```json +{ + "AppSettings": { + "SiteTitle": "ASP.NET Core SessionState Sample App" + }, + + "GeodeSessionStateCache": { + "RegionName": "geodeSessionState", + "Host": "localhost", + "Port": 10334 + } +} +``` + +The ConfigureServices member of the Startup class can be used as follows to retrieve the GeodeSessionStateCache settings from the JSON file: + +```c# +public void ConfigureServices(IServiceCollection services) +{ + ... + services.Configure( + Configuration.GetSection("GeodeSessionStateCache")); + ... +} +``` diff --git a/netcore/AspNetCore GeodeSession Sample/Startup.cs b/netcore/AspNetCore GeodeSession Sample/Startup.cs new file mode 100644 index 0000000000..cb9ada2aec --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/Startup.cs @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using Apache.Geode.Session; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Hosting; +using SessionSample.Middleware; +using Microsoft.Extensions.Caching.Distributed; +using System; + +namespace SessionSample { + public class Startup { + public Startup(IConfiguration configuration) { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + public ILoggerFactory LoggerFactory { get; } + + public void ConfigureServices(IServiceCollection services) { + services.Add(ServiceDescriptor.Singleton()); + + services.AddSession(options => { + options.IdleTimeout = TimeSpan.FromSeconds(5); + options.Cookie.HttpOnly = true; + options.Cookie.IsEssential = true; + }); + + // Configure Method 1: Using extension method: + services.AddGeodeSessionStateCache(configuration => { + configuration.Host = "localhost"; + configuration.Port = 10334; + configuration.RegionName = "geodeSessionState"; + }); + + // Configure Method 2: Using appsettings.json: + // services.Configure( + // Configuration.GetSection("GeodeSessionStateCache")); + + services.AddControllersWithViews(); + services.AddRazorPages(); + } + + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { + if (env.IsDevelopment()) { + app.UseDeveloperExceptionPage(); + } else { + app.UseExceptionHandler("/Home/Error"); + app.UseHsts(); + } + + app.UseStaticFiles(); + app.UseRouting(); + app.UseHttpContextItemsMiddleware(); + app.UseSession(); + app.UseEndpoints(endpoints => { + endpoints.MapDefaultControllerRoute(); + endpoints.MapRazorPages(); + }); + } + } +} diff --git a/netcore/AspNetCore GeodeSession Sample/appsettings.Development.json b/netcore/AspNetCore GeodeSession Sample/appsettings.Development.json new file mode 100644 index 0000000000..0623a3f445 --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/netcore/AspNetCore GeodeSession Sample/appsettings.Production.json b/netcore/AspNetCore GeodeSession Sample/appsettings.Production.json new file mode 100644 index 0000000000..8af1e1fedd --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/appsettings.Production.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Error", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/netcore/AspNetCore GeodeSession Sample/appsettings.json b/netcore/AspNetCore GeodeSession Sample/appsettings.json new file mode 100644 index 0000000000..8205b266fd --- /dev/null +++ b/netcore/AspNetCore GeodeSession Sample/appsettings.json @@ -0,0 +1,17 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Warning" + } + }, + "AllowedHosts": "*", + "AppSettings": { + "SiteTitle": "ASP.NET Core Geode SessionState Sample App" + }, + + "GeodeSessionStateCache": { + "RegionName": "geodeSessionState", + "Host": "localhost", + "Port": 10334 + } +} diff --git a/netcore/geode-dotnet-core.sln b/netcore/geode-dotnet-core.sln index 164413c087..d44f0768aa 100644 --- a/netcore/geode-dotnet-core.sln +++ b/netcore/geode-dotnet-core.sln @@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCore.Session", "NetCore. EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCore.Session.IntegrationTests", "NetCore.Session.IntegrationTests\NetCore.Session.IntegrationTests.csproj", "{94D2CD59-A5F3-4504-BF01-0A3B95CE12B5}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNetCore GeodeSession Sample", "AspNetCore GeodeSession Sample\AspNetCore GeodeSession Sample.csproj", "{4E830BC8-D1CA-40AF-9C9C-D15E193C6585}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -35,6 +37,10 @@ Global {94D2CD59-A5F3-4504-BF01-0A3B95CE12B5}.Debug|x64.Build.0 = Debug|x64 {94D2CD59-A5F3-4504-BF01-0A3B95CE12B5}.Release|x64.ActiveCfg = Release|x64 {94D2CD59-A5F3-4504-BF01-0A3B95CE12B5}.Release|x64.Build.0 = Release|x64 + {4E830BC8-D1CA-40AF-9C9C-D15E193C6585}.Debug|x64.ActiveCfg = Debug|x64 + {4E830BC8-D1CA-40AF-9C9C-D15E193C6585}.Debug|x64.Build.0 = Debug|x64 + {4E830BC8-D1CA-40AF-9C9C-D15E193C6585}.Release|x64.ActiveCfg = Release|x64 + {4E830BC8-D1CA-40AF-9C9C-D15E193C6585}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -42,6 +48,7 @@ Global GlobalSection(NestedProjects) = preSolution {B88C58EB-B144-403B-85F7-7A5B45E643E3} = {520C96EC-F929-4365-8D78-CC5785419B62} {94D2CD59-A5F3-4504-BF01-0A3B95CE12B5} = {520C96EC-F929-4365-8D78-CC5785419B62} + {4E830BC8-D1CA-40AF-9C9C-D15E193C6585} = {520C96EC-F929-4365-8D78-CC5785419B62} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {B30A49F0-1C96-4D6C-A222-0088B1D7FBBE} diff --git a/netcore/scripts/startserver.ps1 b/netcore/scripts/startserver.ps1 index 704be3ed88..232141872f 100644 --- a/netcore/scripts/startserver.ps1 +++ b/netcore/scripts/startserver.ps1 @@ -52,14 +52,16 @@ if ($GFSH_PATH -ne "") $expression = "$GFSH_PATH " + ` "-e 'start locator --name=locator --port=10334 --http-service-port=6060 --J=-Dgemfire.jmx-manager-port=1099' " + ` "-e 'start server --name=server --server-port=0' " + ` - "-e 'create region --name=exampleRegion --type=PARTITION'"; + "-e 'create region --name=exampleRegion --type=PARTITION' " + ` + "-e 'create region --name=geodeSessionState --type=PARTITION'"; Invoke-Expression $expression $expression = "$GFSH_PATH " + ` "-e 'start locator --name=auth_locator $AUTH_LOCATOR_OPTS --port=10335 --http-service-port=7070 --J=-Dgemfire.jmx-manager-port=2099' " + ` "-e 'connect --locator=localhost[10335] --user=server --password=server' " + ` "-e 'start server --name=auth_server $AUTH_OPTS --server-port=0' " + ` - "-e 'create region --name=authExampleRegion --type=PARTITION'"; + "-e 'create region --name=authExampleRegion --type=PARTITION' " + ` + "-e 'create region --name=authGeodeSessionState --type=PARTITION'"; Invoke-Expression $expression } - + \ No newline at end of file diff --git a/netcore/scripts/startserver.sh b/netcore/scripts/startserver.sh index 10d8283a14..3444d9fca5 100644 --- a/netcore/scripts/startserver.sh +++ b/netcore/scripts/startserver.sh @@ -43,6 +43,6 @@ AUTH_OPTS="${AUTH_OPTS} --classpath=${NETCORE_JAVA_BUILD_HOME}/example.jar" AUTH_LOCATOR_OPTS="${AUTH_OPTS} --J=-Dgemfire.security-manager=javaobject.SimpleSecurityManager" -$GFSH_PATH -e "start locator --name=locator --port=10334 --http-service-port=6060 --J=-Dgemfire.jmx-manager-port=1099" -e "start server --name=server --server-port=0" -e "create region --name=exampleRegion --type=PARTITION" +$GFSH_PATH -e "start locator --name=locator --port=10334 --http-service-port=6060 --J=-Dgemfire.jmx-manager-port=1099" -e "start server --name=server --server-port=0" -e "create region --name=exampleRegion --type=PARTITION" -e "create region --name=geodeSessionState --type=PARTITION" -$GFSH_PATH -e "start locator --name=auth_locator ${AUTH_LOCATOR_OPTS} --port=10335 --http-service-port=7070 --J=-Dgemfire.jmx-manager-port=2099" -e "connect --locator=localhost[10335] --user=server --password=server" -e "start server --name=auth_server ${AUTH_OPTS} --server-port=0" -e "create region --name=authExampleRegion --type=PARTITION" +$GFSH_PATH -e "start locator --name=auth_locator ${AUTH_LOCATOR_OPTS} --port=10335 --http-service-port=7070 --J=-Dgemfire.jmx-manager-port=2099" -e "connect --locator=localhost[10335] --user=server --password=server" -e "start server --name=auth_server ${AUTH_OPTS} --server-port=0" -e "create region --name=authExampleRegion --type=PARTITION" -e "create region --name=authGeodeSessionState --type=PARTITION"