-
Notifications
You must be signed in to change notification settings - Fork 69
/
BasicAuthenticationOptions.cs
119 lines (102 loc) · 4.37 KB
/
BasicAuthenticationOptions.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
// Copyright (c) Barry Dorrans. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Authentication;
namespace idunno.Authentication.Basic
{
/// <summary>
/// Contains the options used by the BasicAuthenticationMiddleware
/// </summary>
/// <summary>
/// Contains the options used by the BasicAuthenticationMiddleware
/// </summary>
public class BasicAuthenticationOptions : AuthenticationSchemeOptions
{
private string _realm;
/// <summary>
/// Create an instance of the options initialized with the default values
/// </summary>
public BasicAuthenticationOptions()
{
}
/// <summary>
/// Gets or sets the Realm sent in the WWW-Authenticate header.
/// </summary>
/// <remarks>
/// The realm value (case-sensitive), in combination with the canonical root URL
/// of the server being accessed, defines the protection space.
/// These realms allow the protected resources on a server to be partitioned into a
/// set of protection spaces, each with its own authentication scheme and/or
/// authorization database.
/// </remarks>
public string Realm
{
get
{
return _realm;
}
set
{
if (!string.IsNullOrEmpty(value) && !IsAscii(value))
{
throw new ArgumentException("Realm must be US ASCII");
}
_realm = value;
}
}
/// <summary>
/// Gets or sets the a flag indicating if the WWW-Authenticate header will be suppressed on Unauthorized responses.
/// </summary>
/// <remarks>
/// The authentication scheme controls the browser UI and allows the browser to
/// authenticate in the correct manner, popping up a UI to allow for user name and password.
/// Some users may want to suppress this behaviour for JavaScript XMLHttpRequest requests.
/// Setting this flag to true suppresses the WWW-Authenticate header and thus the browser login prompt, just sending a
/// 401 status code that you must react to yourself in your client code.
/// </remarks>
public bool SuppressWWWAuthenticateHeader { get; set; } = false;
/// <summary>
/// Gets or sets a flag indicating if the handler will prompt for authentication on HTTP requests.
/// </summary>
/// <remarks>
/// If you set this to true you're a horrible person.
/// </remarks>
public bool AllowInsecureProtocol { get; set; } = false;
/// <summary>
/// Sets or sets a value indicating which Encoding method(s) should be used when
/// decoding the user name and/or password specified on the incoming Authorization header.
/// </summary>
/// <remarks>
/// The default value for this setting is Unicode.
/// </remarks>
public EncodingPreference EncodingPreference { get; set; } = EncodingPreference.Utf8;
/// <summary>
/// Sets or sets a value indicating whether the <see cref="EncodingPreference"/>
/// is appended to the WWW-Authenticate header sent to the client.
/// If either of the Prefer values are used for encoding preference only the
/// first encoding will be advertised.
/// </summary>
public bool AdvertiseEncodingPreference { get; set; } = false;
/// <summary>
/// The object provided by the application to process events raised by the basic authentication middleware.
/// The application may implement the interface fully, or it may create an instance of BasicAuthenticationEvents
/// and assign delegates only to the events it wants to process.
/// </summary>
public new BasicAuthenticationEvents Events
{
get { return (BasicAuthenticationEvents)base.Events; }
set { base.Events = value; }
}
private static bool IsAscii(string input)
{
foreach (char c in input)
{
if (c < 32 || c >= 127)
{
return false;
}
}
return true;
}
}
}