-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathMyUserNameTokenProvider.cs
More file actions
73 lines (61 loc) · 2.44 KB
/
MyUserNameTokenProvider.cs
File metadata and controls
73 lines (61 loc) · 2.44 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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
namespace CoreWcf.Samples.TokenProvider
{
class MyUserNameTokenProvider : SecurityTokenProvider
{
static string GetUserName()
{
Console.WriteLine("Username authentication required.");
Console.WriteLine(" Enter username:");
string username = Console.ReadLine();
return username;
}
static string GetPassword()
{
Console.WriteLine("Enter password:");
string password = "";
ConsoleKeyInfo info = Console.ReadKey(true);
while (info.Key != ConsoleKey.Enter)
{
if (info.Key != ConsoleKey.Backspace)
{
password += info.KeyChar;
info = Console.ReadKey(true);
}
else if (info.Key == ConsoleKey.Backspace)
{
if (password != "")
{
password = password.Substring(0, password.Length - 1);
}
info = Console.ReadKey(true);
}
}
for (int i = 0; i < password.Length; i++)
Console.Write("*");
Console.WriteLine();
return password;
}
protected override Task<SecurityToken> GetTokenCoreAsync(TimeSpan timeout)
{
// obtain username and password from the user using console window
string username = GetUserName();
string password = GetPassword();
Console.WriteLine("username: {0}", username);
// return UserNameSecurityToken containing information obtained from user
return Task.FromResult<SecurityToken>(new UserNameSecurityToken(username, password));
}
protected override SecurityToken GetTokenCore(TimeSpan timeout)
{
// obtain username and password from the user using console window
string username = GetUserName();
string password = GetPassword();
Console.WriteLine("username: {0}", username);
// return UserNameSecurityToken containing information obtained from user
return new UserNameSecurityToken(username, password);
}
}
}