/
RegistrationController.cs
202 lines (169 loc) · 7.32 KB
/
RegistrationController.cs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Caching;
using System.Text;
using System.Web.Http;
using WebAPIService.Models;
namespace WebAPIService.Controllers
{
/// <summary>
/// RegistrationController
/// Controller responsible for registering users
/// to use the Web API methods that require
/// authentication
/// </summary>
public class RegistrationController : ApiController
{
#region Fields and Properties
// Memory Cache
ObjectCache cache = MemoryCache.Default;
// List of Registered Users
public List<User> RegisteredUsers { get; private set; }
// Represents the Authentication Scheme
private const string SCHEME = "Basic";
#endregion Fields and Properties
/// <summary>
/// RegistrationController
/// Constructor
/// Cache simulates calls to/from database backend
/// </summary>
public RegistrationController()
{
// Cache for Registered Users for Web API Authentication
if (!cache.Contains("RegisteredUsers"))
{
// Simple List of People for CRUD Example
RegisteredUsers = new List<User>();
// Add Admin Username/Password
RegisteredUsers.Add(new Models.User("admin", "god"));
// Add Generic Test User
RegisteredUsers.Add(new Models.User("testuser", "password"));
// Cache Expiration set to 30 minutes in the future
CacheItemPolicy policy = new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(30) };
// Add a new Cache!
cache.Add("RegisteredUsers", RegisteredUsers, policy);
} // end of if
} // end of method
/// <summary>
/// GET: api/Registration
/// Gets an IEnumerable list of type string
/// registered users in this Web API application
/// </summary>
/// <returns>IEnumerable list of type string</returns>
public IEnumerable<string> Get()
{
RegisteredUsers = (List<User>)cache.Get("RegisteredUsers");
return RegisteredUsers.Select(s => s.ToString()).ToList();
} // end of method
/// <summary>
/// GET: api/Registration/5
/// Gets a single string value of a registered user
/// in the index of the list. Returns a bad request
/// if the index is out of range.
/// </summary>
/// <param name="id">index of the registered user</param>
/// <returns>string of the registered user at the index</returns>
public string Get(int id)
{
// Get the List of Entities from the Cache
RegisteredUsers = (List<User>)cache.Get("RegisteredUsers");
// Don't Process if ID is out of Range 0-entity,count
if (id >= RegisteredUsers.Count || id < 0)
{
// Make a bad response and throw it
HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.BadRequest);
throw new HttpResponseException(message);
}
else
{
return RegisteredUsers[id].ToString();
}
} // end of method
/// <summary>
/// POST: api/Registration
/// This method extracts the username and password
/// from the authorization header in the http request
/// and adds it to a list of registered users
/// </summary>
/// <param name="value">not used</param>
public void Post([FromBody]string value)
{
// Get current request
HttpRequestMessage request = this.Request;
// Extract the headers from the request
HttpRequestHeaders headers = request.Headers;
// Check to see if the authorization header is not null AND
// Check to see if the authorization scheme matches our design (ex: Basic)
if (headers.Authorization != null && SCHEME.Equals(headers.Authorization.Scheme))
{
// Try to parse the encoded username and password
// Assume user id and password Base64 encoded as "userid:password"
// Assume the encoding scheme
Encoding encoding = Encoding.GetEncoding("iso-8859-1");
// Takes the information out of the authorization header
// Convert from a Base64 Encoded string to a regular string
string credentials = encoding.GetString(Convert.FromBase64String(headers.Authorization.Parameter));
// Split the information
string[] parts = credentials.Split(':');
// obtain the username
string username = parts[0].Trim();
// obtain the password
string password = parts[1].Trim();
// Verify User is not already registered
// First try to find a registered user from the Memory Cache
// Cache for Registered Users for Web API Authentication
if (cache.Contains("RegisteredUsers"))
{
// Get the List of Entities from the Cache
RegisteredUsers = (List<User>)cache.Get("RegisteredUsers");
// Try to find an existing user in the registered users
User user = RegisteredUsers.FirstOrDefault(x => x.UserName == username);
if (user == null)
{
// Create a new user
user = new User(username, password);
// Add to the cache
RegisteredUsers.Add(user);
}
else
{
// Duplicate User
// Make a bad response and throw it
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.Conflict);
response.ReasonPhrase = "User already registered. Please choose a different username.";
throw new HttpResponseException(response);
}
} // end of if
} // end of if
else
{
// Make a bad response and throw it
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.BadRequest);
response.ReasonPhrase = "Request authorization header cannot be null or must use BASIC scheme.";
response.Headers.WwwAuthenticate.Add(new AuthenticationHeaderValue(SCHEME));
throw new HttpResponseException(response);
}
} // end of method
//// PUT: api/Registration/5
///// <summary>
/////
///// </summary>
///// <param name="id"></param>
///// <param name="value"></param>
//public void Put(int id, [FromBody]string value)
//{
//}
//// DELETE: api/Registration/5
///// <summary>
/////
///// </summary>
///// <param name="id"></param>
//public void Delete(int id)
//{
//}
} // end of class
} // end of namespace