forked from nabehiro/HttpAuthModule
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCredentialAuthStrategy.cs
More file actions
58 lines (53 loc) · 2.08 KB
/
Copy pathCredentialAuthStrategy.cs
File metadata and controls
58 lines (53 loc) · 2.08 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
using System;
using System.Configuration;
using System.Linq;
using System.Web;
namespace HttpAuthModule
{
/// <summary>
/// Implements the Credentials authentication strategy.
/// </summary>
internal abstract class CredentialAuthStrategy : IAuthStrategy
{
protected string Realm { get; set; }
protected Credential[] Credentials { get; set; }
/// <summary>
/// Initializes a new instance of the
/// <see cref="CredentialAuthStrategy"/> class.
/// </summary>
public CredentialAuthStrategy()
{
Realm = Config.Get("Realm", "SecureZone");
Credentials = Config.Get("Credentials")
.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(str =>
{
var array = str.Trim().Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
if (array.Length != 2) throw new InvalidOperationException("Credentials is invalid.");
return new Credential { Name = array[0], Password = array[1] };
}).ToArray();
if (Credentials.Length == 0)
throw new InvalidOperationException("Credentials is invalid.");
}
/// <inheritdoc/>
public abstract bool Execute(HttpApplication app);
/// <summary>
/// Sends a 401 HTTP status code response to the request.
/// </summary>
/// <param name="app">
/// The HTTP application.
/// </param>
/// <param name="wwwAuthenticate">
/// The WWW-Authenticate header.
/// </param>
protected void Respond401(HttpApplication app, string wwwAuthenticate)
{
app.Context.Response.Clear();
app.Context.Response.Status = "401 Unauthorized";
app.Context.Response.StatusCode = 401;
app.Context.Response.AddHeader("WWW-Authenticate", wwwAuthenticate);
app.Context.Response.SuppressFormsAuthenticationRedirect = true;
app.Context.Response.End();
}
}
}