-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GEODE-9464: Add Asp.Net Core session state sample application (#856)
* Add AspNetCore GeodeSession Sample * Create region for sessionState in startserver * Add Apache license header
- Loading branch information
Showing
23 changed files
with
948 additions
and
5 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
netcore/AspNetCore GeodeSession Sample/AspNetCore GeodeSession Sample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<Platforms>x64</Platforms> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\NetCore.Session\NetCore.Session.csproj" /> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |
6 changes: 6 additions & 0 deletions
6
netcore/AspNetCore GeodeSession Sample/AspNetCore GeodeSession Sample.csproj.user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<ActiveDebugProfile>IIS Express</ActiveDebugProfile> | ||
</PropertyGroup> | ||
</Project> |
53 changes: 53 additions & 0 deletions
53
netcore/AspNetCore GeodeSession Sample/Extensions/SessionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T>(this ISession session, string key, T value) { | ||
session.SetString(key, JsonSerializer.Serialize(value)); | ||
} | ||
|
||
public static T Get<T>(this ISession session, string key) { | ||
var value = session.GetString(key); | ||
return value == null ? default : JsonSerializer.Deserialize<T>(value); | ||
} | ||
} | ||
#endregion | ||
} | ||
|
||
namespace Web.Extensions2 { | ||
// Alternate approach | ||
|
||
public static class SessionExtensions { | ||
public static void Set<T>(this ISession session, string key, T value) { | ||
session.SetString(key, JsonSerializer.Serialize(value)); | ||
} | ||
|
||
public static bool TryGet<T>(this ISession session, string key, out T value) { | ||
var state = session.GetString(key); | ||
value = default; | ||
if (state == null) { | ||
return false; | ||
} | ||
value = JsonSerializer.Deserialize<T>(state); | ||
return true; | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
netcore/AspNetCore GeodeSession Sample/Middleware/HttpContextItemsMiddleware.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<HttpContextItemsMiddleware>(); | ||
} | ||
} | ||
#endregion | ||
} |
47 changes: 47 additions & 0 deletions
47
netcore/AspNetCore GeodeSession Sample/Models/BasicAuthInitialize.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string, string> GetCredentials() | ||
{ | ||
Console.WriteLine("SimpleAuthInitialize::GetCredentials called"); | ||
var credentials = new Dictionary<string, string>(); | ||
credentials.Add("security-username", "root"); | ||
credentials.Add("security-password", "root-password"); | ||
return credentials; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
netcore/AspNetCore GeodeSession Sample/Models/ErrorViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
|
||
<h1 class="text-danger">Error.</h1> | ||
<h2 class="text-danger">An error occurred while processing your request.</h2> | ||
|
||
@if (Model.ShowRequestId) | ||
{ | ||
<p> | ||
<strong>Request ID:</strong> <code>@Model.RequestId</code> | ||
</p> | ||
} | ||
|
||
<h3>Development Mode</h3> | ||
<p> | ||
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred. | ||
</p> | ||
<p> | ||
<strong>The Development environment shouldn't be enabled for deployed applications.</strong> | ||
It can result in displaying sensitive information from exceptions to end users. | ||
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong> | ||
and restarting the app. | ||
</p> |
36 changes: 36 additions & 0 deletions
36
netcore/AspNetCore GeodeSession Sample/Pages/Error.cshtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
|
||
<h1>@ViewData["Title"]</h1> | ||
|
||
<h2>State management</h2> | ||
|
||
<div class="row"> | ||
<div class="col-md-8"> | ||
<form method="post"> | ||
<div class="panel panel-default"> | ||
<div class="panel-heading"> | ||
<button type="submit" asp-page-handler="ChangeAge" class="pull-right btn btn-danger">Change Age</button> | ||
<h3 class="panel-title" style="line-height:2.1">Name and Age</h3> | ||
</div> | ||
<div class="panel-body"> | ||
<p> | ||
The name and age are stored in session. Select the <span style="font-weight:bold">Change Age</span> | ||
button to update the session to a new random age value. | ||
</p> | ||
<p> | ||
Session values by the model with | ||
<code>@@Model.<PropertyName></code>: | ||
</p> | ||
<p><b>Name:</b> @Model.SessionInfo_Name <b>Age:</b> @Model.SessionInfo_Age</p> | ||
<hr> | ||
<p>Session values direct </p> | ||
<p> | ||
<b>Name:</b> @HttpContext.Session.GetString(IndexModel.SessionKeyName) | ||
<b>Age:</b> | ||
@HttpContext.Session.GetInt32(IndexModel.SessionKeyAge).ToString() | ||
</p> | ||
</div> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
|
||
<div class="row"> | ||
<div class="col-md-8"> | ||
<div class="panel panel-default"> | ||
<div class="panel-heading"> | ||
<h3 class="panel-title">HttpContext.Items Middleware Value</h3> | ||
</div> | ||
<div class="panel-body"> | ||
<p> | ||
The middleware value is set into the <code>HttpContext.Items</code> collection by | ||
the <code>HttpContextItemsMiddleware</code> before Razor Pages processes the request. | ||
The value is retreived by the page and displayed. | ||
</p> | ||
<p>Value: @Model.SessionInfo_MiddlewareValue</p> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="row"> | ||
<div class="col-md-8"> | ||
<form method="post"> | ||
<div class="panel panel-default"> | ||
<div class="panel-heading clearfix"> | ||
<button type="submit" asp-page-handler="UpdateSessionDate" class="pull-right btn btn-danger">Update Session Time</button> | ||
<a href="/" class="pull-right btn btn-danger" style="margin-right:5px">Reload Page (No Update)</a> | ||
<h3 class="panel-title" style="line-height:2.1">Session Time</h3> | ||
</div> | ||
<div class="panel-body"> | ||
<p> | ||
The session time is stored in session. Select the <span style="font-weight:bold"> | ||
Reload Page (No Update) | ||
</span> button to display the current time and the time stored in session. | ||
Select the <span style="font-weight:bold">Update Session Time</span> button to store the current time in session. | ||
</p> | ||
<p>Current Time: @Model.SessionInfo_CurrentTime</p> | ||
<p>Session Time: @Model.SessionInfo_SessionTime</p> | ||
</div> | ||
</div> | ||
</form> | ||
</div> | ||
</div> |
Oops, something went wrong.