Skip to content

Commit cf2a4ae

Browse files
send auth token in login page response
1 parent 8488a4e commit cf2a4ae

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

App_Start/Extensions/Extensions.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Security.Claims;
5-
using System.Threading.Tasks;
1+
using Microsoft.AspNetCore.Authentication.Cookies;
62
using Microsoft.AspNetCore.Http;
73
using Microsoft.AspNetCore.Mvc.Rendering;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Options;
86
using Olive;
97
using Olive.Microservices.Hub;
108
using Olive.Security;
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
12+
using System.Security.Claims;
13+
using System.Threading.Tasks;
1114

1215
namespace System
1316
{
@@ -16,7 +19,7 @@ public static class Extensions
1619
static int Timeout => Config.Get<int>("Authentication:Cookie:Timeout");
1720
static int MobileTimeout => Config.Get<int>("Authentication:Cookie:TimeoutMobile");
1821

19-
public static Task LogOn(this PeopleService.UserInfo @this)
22+
public static async Task LogOn(this PeopleService.UserInfo @this)
2023
{
2124
var mobile = Context.Current.Request().IsSmartPhone();
2225

@@ -31,7 +34,26 @@ public static Task LogOn(this PeopleService.UserInfo @this)
3134

3235
TryAddJwtToken(loggingInfo, mobile);
3336

34-
return loggingInfo.LogOn(remember: mobile);
37+
await loggingInfo.LogOn(remember: mobile);
38+
39+
Context.Current.Http().SetAuthHeader();
40+
}
41+
42+
public static void SetAuthHeader(this HttpContext httpContext)
43+
{
44+
var cookieOptions = httpContext.RequestServices.GetService<IOptionsMonitor<CookieAuthenticationOptions>>();
45+
var cookieName = cookieOptions?.Get(CookieAuthenticationDefaults.AuthenticationScheme).Cookie.Name;
46+
47+
if (cookieName.IsEmpty())
48+
{
49+
return;
50+
}
51+
52+
var authCookie = httpContext.Response.Headers["Set-Cookie"]
53+
.FirstOrDefault(h => h.StartsWith($"{cookieName}="));
54+
55+
var token = authCookie?.Split(';')[0].Split('=')[1];
56+
httpContext.Response.Headers["X-Auth-Token"] = token;
3557
}
3658

3759
public static async Task<PeopleService.UserInfo> LoadUser(this ClaimsPrincipal principal)

Olive.Microservices.Hub.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<Authors>Geeks Ltd</Authors>
99
<RepositoryUrl>https://github.com/Geeksltd/Olive.Microservices.Hub/tree/master/Olive.Microservices.Hub</RepositoryUrl>
1010
<PackageIcon>icon.png</PackageIcon>
11-
<Version>8.0.3</Version>
11+
<Version>8.1.0</Version>
1212
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
1313
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
1414
<Nullable>warnings</Nullable>

0 commit comments

Comments
 (0)