Skip to content

Commit e2f8d0f

Browse files
xljiulangchkr1011
andauthored
Added UserName property to various Server EventArgs. (#2123)
* Added UserName property to various Server EventArgs. * InjectedMqttApplicationMessage: Add SenderUserName property. * Reformat code * Reformat code --------- Co-authored-by: christian <6939810+chkr1011@users.noreply.github.com>
1 parent 981ea02 commit e2f8d0f

17 files changed

+116
-28
lines changed

Source/MQTTnet.Server/Events/ClientAcknowledgedPublishPacketEventArgs.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ namespace MQTTnet.Server
1010
{
1111
public sealed class ClientAcknowledgedPublishPacketEventArgs : EventArgs
1212
{
13-
public ClientAcknowledgedPublishPacketEventArgs(string clientId, IDictionary sessionItems, MqttPublishPacket publishPacket, MqttPacketWithIdentifier acknowledgePacket)
13+
public ClientAcknowledgedPublishPacketEventArgs(string clientId, string userName, IDictionary sessionItems, MqttPublishPacket publishPacket, MqttPacketWithIdentifier acknowledgePacket)
1414
{
1515
ClientId = clientId ?? throw new ArgumentNullException(nameof(clientId));
16+
UserName = userName;
1617
SessionItems = sessionItems ?? throw new ArgumentNullException(nameof(sessionItems));
1718
PublishPacket = publishPacket ?? throw new ArgumentNullException(nameof(publishPacket));
1819
AcknowledgePacket = acknowledgePacket ?? throw new ArgumentNullException(nameof(acknowledgePacket));
@@ -28,6 +29,11 @@ public ClientAcknowledgedPublishPacketEventArgs(string clientId, IDictionary ses
2829
/// </summary>
2930
public string ClientId { get; }
3031

32+
/// <summary>
33+
/// Gets the user name of the client.
34+
/// </summary>
35+
public string UserName { get; }
36+
3137
/// <summary>
3238
/// Gets whether the PUBLISH packet is fully acknowledged. This is the case for PUBACK (QoS 1) and PUBCOMP (QoS 2.
3339
/// </summary>

Source/MQTTnet.Server/Events/ClientConnectedEventArgs.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections;
77
using System.Collections.Generic;
88
using System.Net;
9+
using System.Text;
910
using MQTTnet.Formatter;
1011
using MQTTnet.Packets;
1112

@@ -56,6 +57,11 @@ public ClientConnectedEventArgs(MqttConnectPacket connectPacket, MqttProtocolVer
5657
/// </summary>
5758
public string UserName => _connectPacket.Username;
5859

60+
/// <summary>
61+
/// Gets the password of the connected client.
62+
/// </summary>
63+
public string Password => Encoding.UTF8.GetString(_connectPacket.Password.AsSpan());
64+
5965
/// <summary>
6066
/// Gets the user properties sent by the client.
6167
/// <remarks>MQTT 5.0.0+ feature.</remarks>

Source/MQTTnet.Server/Events/ClientDisconnectedEventArgs.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,29 @@
66
using System.Collections;
77
using System.Collections.Generic;
88
using System.Net;
9+
using System.Text;
910
using MQTTnet.Packets;
1011
using MQTTnet.Protocol;
1112

1213
namespace MQTTnet.Server
1314
{
1415
public sealed class ClientDisconnectedEventArgs : EventArgs
1516
{
17+
readonly MqttConnectPacket _connectPacket;
1618
readonly MqttDisconnectPacket _disconnectPacket;
1719

1820
public ClientDisconnectedEventArgs(
19-
string clientId,
21+
MqttConnectPacket connectPacket,
2022
MqttDisconnectPacket disconnectPacket,
2123
MqttClientDisconnectType disconnectType,
2224
EndPoint remoteEndPoint,
2325
IDictionary sessionItems)
2426
{
25-
ClientId = clientId ?? throw new ArgumentNullException(nameof(clientId));
2627
DisconnectType = disconnectType;
2728
RemoteEndPoint = remoteEndPoint;
2829
SessionItems = sessionItems ?? throw new ArgumentNullException(nameof(sessionItems));
2930

31+
_connectPacket = connectPacket;
3032
// The DISCONNECT packet can be null in case of a non clean disconnect or session takeover.
3133
_disconnectPacket = disconnectPacket;
3234
}
@@ -35,7 +37,17 @@ public ClientDisconnectedEventArgs(
3537
/// Gets the client identifier.
3638
/// Hint: This identifier needs to be unique over all used clients / devices on the broker to avoid connection issues.
3739
/// </summary>
38-
public string ClientId { get; }
40+
public string ClientId => _connectPacket.ClientId;
41+
42+
/// <summary>
43+
/// Gets the user name of the client.
44+
/// </summary>
45+
public string UserName => _connectPacket.Username;
46+
47+
/// <summary>
48+
/// Gets the password of the client.
49+
/// </summary>
50+
public string Password => Encoding.UTF8.GetString(_connectPacket.Password.AsSpan());
3951

4052
public MqttClientDisconnectType DisconnectType { get; }
4153

Source/MQTTnet.Server/Events/ClientSubscribedTopicEventArgs.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ namespace MQTTnet.Server
1010
{
1111
public sealed class ClientSubscribedTopicEventArgs : EventArgs
1212
{
13-
public ClientSubscribedTopicEventArgs(string clientId, MqttTopicFilter topicFilter, IDictionary sessionItems)
13+
public ClientSubscribedTopicEventArgs(string clientId, string userName, MqttTopicFilter topicFilter, IDictionary sessionItems)
1414
{
1515
ClientId = clientId ?? throw new ArgumentNullException(nameof(clientId));
16+
UserName = userName;
1617
TopicFilter = topicFilter ?? throw new ArgumentNullException(nameof(topicFilter));
1718
SessionItems = sessionItems ?? throw new ArgumentNullException(nameof(sessionItems));
1819
}
@@ -23,6 +24,11 @@ public ClientSubscribedTopicEventArgs(string clientId, MqttTopicFilter topicFilt
2324
/// </summary>
2425
public string ClientId { get; }
2526

27+
/// <summary>
28+
/// Gets the user name of the client.
29+
/// </summary>
30+
public string UserName { get; }
31+
2632
/// <summary>
2733
/// Gets or sets a key/value collection that can be used to share data within the scope of this session.
2834
/// </summary>

Source/MQTTnet.Server/Events/ClientUnsubscribedTopicEventArgs.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ namespace MQTTnet.Server
99
{
1010
public sealed class ClientUnsubscribedTopicEventArgs : EventArgs
1111
{
12-
public ClientUnsubscribedTopicEventArgs(string clientId, string topicFilter, IDictionary sessionItems)
12+
public ClientUnsubscribedTopicEventArgs(string clientId, string userName, string topicFilter, IDictionary sessionItems)
1313
{
1414
ClientId = clientId ?? throw new ArgumentNullException(nameof(clientId));
15+
UserName = userName;
1516
TopicFilter = topicFilter ?? throw new ArgumentNullException(nameof(topicFilter));
1617
SessionItems = sessionItems ?? throw new ArgumentNullException(nameof(sessionItems));
1718
}
@@ -22,6 +23,11 @@ public ClientUnsubscribedTopicEventArgs(string clientId, string topicFilter, IDi
2223
/// </summary>
2324
public string ClientId { get; }
2425

26+
/// <summary>
27+
/// Gets the user name of the client.
28+
/// </summary>
29+
public string UserName { get; }
30+
2531
/// <summary>
2632
/// Gets or sets a key/value collection that can be used to share data within the scope of this session.
2733
/// </summary>

Source/MQTTnet.Server/Events/InterceptingPacketEventArgs.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ namespace MQTTnet.Server
1212
{
1313
public sealed class InterceptingPacketEventArgs : EventArgs
1414
{
15-
public InterceptingPacketEventArgs(CancellationToken cancellationToken, string clientId, EndPoint remoteEndPoint, MqttPacket packet, IDictionary sessionItems)
15+
public InterceptingPacketEventArgs(CancellationToken cancellationToken, string clientId, string userName, EndPoint remoteEndPoint, MqttPacket packet, IDictionary sessionItems)
1616
{
1717
CancellationToken = cancellationToken;
1818
ClientId = clientId ?? throw new ArgumentNullException(nameof(clientId));
19+
UserName = userName;
1920
RemoteEndPoint = remoteEndPoint;
2021
Packet = packet ?? throw new ArgumentNullException(nameof(packet));
2122
SessionItems = sessionItems;
@@ -32,6 +33,11 @@ public InterceptingPacketEventArgs(CancellationToken cancellationToken, string c
3233
/// </summary>
3334
public string ClientId { get; }
3435

36+
/// <summary>
37+
/// Gets the user name of the client.
38+
/// </summary>
39+
public string UserName { get; }
40+
3541
/// <summary>
3642
/// Gets the endpoint of the sending or receiving client.
3743
/// </summary>

Source/MQTTnet.Server/Events/InterceptingPublishEventArgs.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ namespace MQTTnet.Server
1010
{
1111
public sealed class InterceptingPublishEventArgs : EventArgs
1212
{
13-
public InterceptingPublishEventArgs(MqttApplicationMessage applicationMessage, CancellationToken cancellationToken, string clientId, IDictionary sessionItems)
13+
public InterceptingPublishEventArgs(MqttApplicationMessage applicationMessage, CancellationToken cancellationToken, string clientId, string userName, IDictionary sessionItems)
1414
{
1515
ApplicationMessage = applicationMessage ?? throw new ArgumentNullException(nameof(applicationMessage));
1616
CancellationToken = cancellationToken;
1717
ClientId = clientId ?? throw new ArgumentNullException(nameof(clientId));
18+
UserName = userName;
1819
SessionItems = sessionItems ?? throw new ArgumentNullException(nameof(sessionItems));
1920
}
2021

@@ -31,6 +32,11 @@ public InterceptingPublishEventArgs(MqttApplicationMessage applicationMessage, C
3132
/// </summary>
3233
public string ClientId { get; }
3334

35+
/// <summary>
36+
/// Gets the user name of the client.
37+
/// </summary>
38+
public string UserName { get; }
39+
3440
public bool CloseConnection { get; set; }
3541

3642
/// <summary>

Source/MQTTnet.Server/Events/InterceptingSubscriptionEventArgs.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ public sealed class InterceptingSubscriptionEventArgs : EventArgs
1515
public InterceptingSubscriptionEventArgs(
1616
CancellationToken cancellationToken,
1717
string clientId,
18+
string userName,
1819
MqttSessionStatus session,
1920
MqttTopicFilter topicFilter,
2021
List<MqttUserProperty> userProperties)
2122
{
2223
CancellationToken = cancellationToken;
2324
ClientId = clientId;
25+
UserName = userName;
2426
Session = session;
2527
TopicFilter = topicFilter;
2628
UserProperties = userProperties;
@@ -37,6 +39,11 @@ public InterceptingSubscriptionEventArgs(
3739
/// </summary>
3840
public string ClientId { get; }
3941

42+
/// <summary>
43+
/// Gets the user name of the client.
44+
/// </summary>
45+
public string UserName { get; }
46+
4047
/// <summary>
4148
/// Gets or sets whether the broker should close the client connection.
4249
/// </summary>

Source/MQTTnet.Server/Events/InterceptingUnsubscriptionEventArgs.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ namespace MQTTnet.Server
1212
{
1313
public sealed class InterceptingUnsubscriptionEventArgs : EventArgs
1414
{
15-
public InterceptingUnsubscriptionEventArgs(CancellationToken cancellationToken, string clientId, IDictionary sessionItems, string topic, List<MqttUserProperty> userProperties)
15+
public InterceptingUnsubscriptionEventArgs(CancellationToken cancellationToken, string clientId, string userName, IDictionary sessionItems, string topic, List<MqttUserProperty> userProperties)
1616
{
1717
CancellationToken = cancellationToken;
1818
ClientId = clientId;
19+
UserName = userName;
1920
SessionItems = sessionItems;
2021
Topic = topic;
2122
UserProperties = userProperties;
@@ -32,6 +33,11 @@ public InterceptingUnsubscriptionEventArgs(CancellationToken cancellationToken,
3233
/// </summary>
3334
public string ClientId { get; }
3435

36+
/// <summary>
37+
/// Gets the user name of the client.
38+
/// </summary>
39+
public string UserName { get; }
40+
3541
/// <summary>
3642
/// Gets or sets whether the broker should close the client connection.
3743
/// </summary>

Source/MQTTnet.Server/Events/SessionDeletedEventArgs.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ namespace MQTTnet.Server
99
{
1010
public sealed class SessionDeletedEventArgs : EventArgs
1111
{
12-
public SessionDeletedEventArgs(string id, IDictionary sessionItems)
12+
public SessionDeletedEventArgs(string id, string userName, IDictionary sessionItems)
1313
{
1414
Id = id ?? throw new ArgumentNullException(nameof(id));
15+
UserName = userName;
1516
SessionItems = sessionItems ?? throw new ArgumentNullException(nameof(sessionItems));
1617
}
1718

@@ -20,6 +21,11 @@ public SessionDeletedEventArgs(string id, IDictionary sessionItems)
2021
/// </summary>
2122
public string Id { get; }
2223

24+
/// <summary>
25+
/// Gets the user name of the session.
26+
/// </summary>
27+
public string UserName { get; }
28+
2329
/// <summary>
2430
/// Gets or sets a key/value collection that can be used to share data within the scope of this session.
2531
/// </summary>

0 commit comments

Comments
 (0)