-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAuthTokenFilterAttribute.cs
More file actions
45 lines (40 loc) · 1.52 KB
/
AuthTokenFilterAttribute.cs
File metadata and controls
45 lines (40 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace PrjFunNowWeb
{
public class AuthTokenFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
// 檢查原始請求是否已包含 token
var tokenInQueryString = context.HttpContext.Request.Query.ContainsKey("token");
// 如果請求不包含 token,則進行重新導向
if (!tokenInQueryString)
{
var token = context.HttpContext.Session.GetString("Token");
// 調試代碼
if (string.IsNullOrEmpty(token))
{
Console.WriteLine("Token is null or empty");
}
else
{
Console.WriteLine($"Token value: {token}");
}
if (!string.IsNullOrEmpty(token))
{
var queryString = context.HttpContext.Request.QueryString.HasValue
? context.HttpContext.Request.QueryString.Value
: string.Empty;
var url = $"{context.HttpContext.Request.Path.Value}?tk={token}{queryString}";
context.Result = new RedirectResult(url);
}
}
else
{
// 如果請求已包含 token,則繼續執行 Action 方法
base.OnActionExecuting(context);
}
}
}
}