Skip to content

Commit f704560

Browse files
committed
增加BasicAuthenticationHeaderValue
1 parent 19b05c3 commit f704560

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

WebApiClientCore/Attributes/ActionAttributes/BasicAuthAttribute.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Diagnostics;
33
using System.Net.Http.Headers;
4-
using System.Text;
54
using System.Threading.Tasks;
65

76
namespace WebApiClientCore.Attributes
@@ -13,26 +12,19 @@ namespace WebApiClientCore.Attributes
1312
public class BasicAuthAttribute : ApiActionAttribute
1413
{
1514
/// <summary>
16-
/// 参数
15+
/// 授权头的值
1716
/// </summary>
18-
private readonly string parameter;
17+
private readonly BasicAuthenticationHeaderValue authorization;
1918

2019
/// <summary>
2120
/// 基本授权
2221
/// </summary>
23-
/// <param name="userName">账号</param>
22+
/// <param name="username">账号</param>
2423
/// <param name="password">密码</param>
2524
/// <exception cref="ArgumentNullException"></exception>
26-
public BasicAuthAttribute(string userName, string? password)
25+
public BasicAuthAttribute(string username, string? password)
2726
{
28-
if (string.IsNullOrEmpty(userName))
29-
{
30-
throw new ArgumentNullException(userName);
31-
}
32-
33-
var value = $"{userName}:{password}";
34-
var bytes = Encoding.ASCII.GetBytes(value);
35-
this.parameter = Convert.ToBase64String(bytes);
27+
this.authorization = new BasicAuthenticationHeaderValue(username, password);
3628
}
3729

3830
/// <summary>
@@ -42,7 +34,7 @@ public BasicAuthAttribute(string userName, string? password)
4234
/// <returns></returns>
4335
public override Task OnRequestAsync(ApiRequestContext context)
4436
{
45-
context.HttpContext.RequestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", this.parameter);
37+
context.HttpContext.RequestMessage.Headers.Authorization = authorization;
4638
return Task.CompletedTask;
4739
}
4840
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System.Text;
2+
3+
namespace System.Net.Http.Headers
4+
{
5+
/// <summary>
6+
/// 表示Basic体系的授权头值
7+
/// </summary>
8+
public class BasicAuthenticationHeaderValue : AuthenticationHeaderValue
9+
{
10+
/// <summary>
11+
/// 获取账号
12+
/// </summary>
13+
public string Username { get; }
14+
15+
/// <summary>
16+
/// 获取密码
17+
/// </summary>
18+
public string? Password { get; }
19+
20+
/// <summary>
21+
/// Basic体系的授权头值
22+
/// </summary>
23+
/// <param name="username">账号</param>
24+
/// <param name="password">密码</param>
25+
public BasicAuthenticationHeaderValue(string username, string? password)
26+
: base("Basic", GetParameter(username, password))
27+
{
28+
this.Username = username;
29+
this.Password = password;
30+
}
31+
32+
/// <summary>
33+
/// 获取参数
34+
/// </summary>
35+
/// <param name="username">账号</param>
36+
/// <param name="password">密码</param>
37+
/// <returns></returns>
38+
private static string GetParameter(string username, string? password)
39+
{
40+
if (string.IsNullOrEmpty(username))
41+
{
42+
throw new ArgumentNullException(username);
43+
}
44+
45+
var value = $"{username}:{password}";
46+
var bytes = Encoding.ASCII.GetBytes(value);
47+
return Convert.ToBase64String(bytes);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)