-
Notifications
You must be signed in to change notification settings - Fork 1
/
HttpBasicAuthenticationResult.cs
49 lines (42 loc) · 1.49 KB
/
HttpBasicAuthenticationResult.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
using System;
using System.Web.Mvc;
using System.Net;
namespace ManagedFusion.Web.Mvc
{
public class HttpBasicAuthenticationResult : ActionResult
{
/// <summary>
/// Initializes a new instance of the <see cref="BasicAuthenticationResult"/> class.
/// </summary>
/// <param name="realm">The realm.</param>
public HttpBasicAuthenticationResult(string realm)
{
Realm = realm;
}
/// <summary>
/// Gets or sets the realm.
/// </summary>
/// <value>The realm.</value>
public string Realm { get; set; }
/// <summary>
/// Enables processing of the result of an action method by a custom type that inherits from <see cref="T:System.Web.Mvc.ActionResult"/>.
/// </summary>
/// <param name="context">The context within which the result is executed.</param>
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
// 401 is the HTTP status code for unauthorized access - setting this
// will cause the active authentication module to execute its default
// unauthorized handler
var response = context.HttpContext.Response;
response.Clear();
response.StatusCode = (int)HttpStatusCode.Unauthorized;
response.StatusDescription = "Unauthorized";
response.AppendHeader("WWW-Authenticate", "Basic realm=\"" + Realm + "\"");
response.ContentType = "text/html";
response.Write("<html><head><title>Unauthorized</title></head><h1>Unauthorized</h1></html>");
response.End();
}
}
}