0%

.net core LINE Login Middleware

 

無意中發現以前寫過Line Login的 Middleware 到底是參考來的還是自己寫的已不可考
前端可以參考董大神

後端 Middleware

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Demo.Middlewares
{

public class LineCallbackResult
{
[JsonPropertyName("access_token")]
public string AccessToken { get; set; }

[JsonPropertyName("token_type")]
public string TokenType { get; set; }

[JsonPropertyName("refresh_token")]
public string RefreshToken { get; set; }

[JsonPropertyName("expires_in")]
public int ExpiresIn { get; set; }

[JsonPropertyName("scope")]
public string Scope { get; set; }

[JsonPropertyName("id_token")]
public string IdToken { get; set; }
}

public class LineLoginMiddleware
{
private readonly RequestDelegate _next;
public LineLoginMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext context)
{
//取得line login callback 的 url query string code
string code = context.Request.Query["code"];

if (code != null)
{
//填寫post需要的line login參數
var dict = new Dictionary<string, string>();
dict.Add("grant_type", "authorization_code");
dict.Add("code", code);
dict.Add("redirect_uri", "http://localhost:5000/index.html");
dict.Add("client_id", "yourid");
dict.Add("client_secret", "yourkey");

//執行post
using (var client = new HttpClient())
{
//注意utf8防止亂碼
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");

var req = new HttpRequestMessage(
HttpMethod.Post,
@"https://api.line.me/oauth2/v2.1/token")
{
Content = new FormUrlEncodedContent(dict)
};
var res = await client.SendAsync(req);
var json = await res.Content.ReadAsStreamAsync();
LineCallbackResult callbackResult =
await JsonSerializer.DeserializeAsync<LineCallbackResult>(json);

Console.WriteLine(callbackResult.AccessToken);
Console.WriteLine(callbackResult.TokenType);
Console.WriteLine(callbackResult.RefreshToken);
Console.WriteLine(callbackResult.ExpiresIn);
Console.WriteLine(callbackResult.Scope);
Console.WriteLine(callbackResult.IdToken);
Console.WriteLine("--------------------");

//Console.WriteLine(callbackResult.access_token);
//Console.WriteLine(callbackResult.token_type);
//Console.WriteLine(callbackResult.refresh_token);
//Console.WriteLine(callbackResult.expires_in);
//Console.WriteLine(callbackResult.scope);
//Console.WriteLine(callbackResult.id_token);
Console.WriteLine("--------------------");

//https://jwt.io/
//解析id_token的功能
var JwtSecurityToken = new System.IdentityModel.Tokens.Jwt.JwtSecurityToken(callbackResult.IdToken);

//打印user的訊息
foreach (var claims in JwtSecurityToken.Claims)
{
Console.WriteLine(@$"{claims.Type} : {claims.Value}");
}
}
}
await _next.Invoke(context);
}
}
}
關閉