Skip to content

Commit d9a8015

Browse files
committed
Add LogoutAsync(string).
1 parent 406eb63 commit d9a8015

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

samples/TsinghuaNet/Program.vb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Module Program
4848
Console.Error.WriteLine("Invalid host.")
4949
Return
5050
End If
51-
Logout(helper).Wait()
51+
Logout(helper, command.Username).Wait()
5252
End If
5353
If command.Flux Then
5454
If helper Is Nothing Then
@@ -83,9 +83,13 @@ Module Program
8383
Console.Error.WriteLine("Exception occured: {0}", ex.Message)
8484
End Try
8585
End Function
86-
Async Function Logout(helper As IConnect) As Task
86+
Async Function Logout(helper As IConnect, username As String) As Task
8787
Try
88-
Console.WriteLine(Await helper.LogoutAsync())
88+
If username Is Nothing Then
89+
Console.WriteLine(Await helper.LogoutAsync())
90+
Else
91+
Console.WriteLine(Await helper.LogoutAsync(username))
92+
End If
8993
Catch ex As Exception
9094
Console.Error.WriteLine("Exception occured: {0}", ex.Message)
9195
End Try

src/Berrysoft.Tsinghua.Net/AuthHelper.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public abstract class AuthHelper : NetHelperBase, IConnect
1414
private const string LogUriBase = "https://auth{0}.tsinghua.edu.cn/cgi-bin/srun_portal";
1515
private const string FluxUriBase = "https://auth{0}.tsinghua.edu.cn/rad_user_info.php";
1616
private const string ChallengeUriBase = "https://auth{0}.tsinghua.edu.cn/cgi-bin/get_challenge?username={{0}}&double_stack=1&ip&callback=callback";
17-
private const string LogoutData = "action=logout&ac_id=1&ip=&double_stack=1";
17+
private const string LogoutData = "action=logout";
18+
private const string LogoutUserData = "action=logout&username={0}";
1819
private readonly string LogUri;
1920
private readonly string FluxUri;
2021
private readonly string ChallengeUri;
@@ -67,6 +68,14 @@ internal AuthHelper(string username, string password, HttpClient client, int ver
6768
/// <returns>The response of the website.</returns>
6869
public Task<string> LogoutAsync() => PostAsync(LogUri, LogoutData);
6970
/// <summary>
71+
/// Logout from the network with the specified username.
72+
/// When a user logged in through <see cref="AuthHelper"/> and logged out through <see cref="NetHelper"/>,
73+
/// he should call this method with his username explicitly, or he can't logout.
74+
/// </summary>
75+
/// <param name="username">The specified username.</param>
76+
/// <returns>The response of the website.</returns>
77+
public Task<string> LogoutAsync(string username) => PostAsync(LogUri, string.Format(LogoutUserData, username));
78+
/// <summary>
7079
/// Get information of the user online.
7180
/// </summary>
7281
/// <returns>An instance of <see cref="FluxUser"/> class of the current user.</returns>

src/Berrysoft.Tsinghua.Net/NetHelper.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class NetHelper : NetHelperBase, IConnect
1313
private const string LogUri = "http://net.tsinghua.edu.cn/do_login.php";
1414
private const string FluxUri = "http://net.tsinghua.edu.cn/rad_user_info.php";
1515
private const string LogoutData = "action=logout";
16+
private const string LogoutUserData = "action=logout&username={0}";
1617
/// <summary>
1718
/// Initializes a new instance of the <see cref="NetHelper"/> class.
1819
/// </summary>
@@ -54,6 +55,12 @@ public NetHelper(string username, string password, HttpClient client)
5455
/// <returns>The response of the website.</returns>
5556
public Task<string> LogoutAsync() => PostAsync(LogUri, LogoutData);
5657
/// <summary>
58+
/// Logout from the network with the specified username.
59+
/// </summary>
60+
/// <param name="username">The specified username.</param>
61+
/// <returns>The response of the website.</returns>
62+
public Task<string> LogoutAsync(string username) => PostAsync(LogUri, string.Format(LogoutUserData, username));
63+
/// <summary>
5764
/// Get information of the user online.
5865
/// </summary>
5966
/// <returns>An instance of <see cref="FluxUser"/> class of the current user.</returns>

src/Berrysoft.Tsinghua.Net/NetHelperBase.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public interface IConnect : IDisposable
2222
/// <returns>The response of the website, may be a sentense or a html page.</returns>
2323
Task<string> LogoutAsync();
2424
/// <summary>
25+
/// Logout from the network with the specified username.
26+
/// </summary>
27+
/// <param name="username">The specified username.</param>
28+
/// <returns>The response of the website, may be a sentense or a html page.</returns>
29+
Task<string> LogoutAsync(string username);
30+
/// <summary>
2531
/// Get information of the user online.
2632
/// </summary>
2733
/// <returns>An instance of <see cref="FluxUser"/> class of the current user.</returns>

0 commit comments

Comments
 (0)