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 203 204 205 206 207 208 209
|
//------------------------------------------------------------------------------
// <copyright file="BackStopAuthenticationModule.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.Security {
using System.Collections.Specialized;
using System.Runtime.Serialization;
using System.Security.Principal;
using System.Security.Permissions;
using System.Threading;
using System.Web;
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public sealed class DefaultAuthenticationModule : IHttpModule {
private DefaultAuthenticationEventHandler _eventHandler;
/// <devdoc>
/// <para>
/// Initializes a new instance of the <see cref='System.Web.Security.DefaultAuthenticationModule'/>
/// class.
/// </para>
/// </devdoc>
[SecurityPermission(SecurityAction.Demand, Unrestricted=true)]
public DefaultAuthenticationModule() {
}
[SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
internal static DefaultAuthenticationModule CreateDefaultAuthenticationModuleWithAssert() {
return new DefaultAuthenticationModule();
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public event DefaultAuthenticationEventHandler Authenticate {
add {
// WOS 1709222: DefaultAuthentication_Authenticate is not supported in integrated mode.
if (HttpRuntime.UseIntegratedPipeline) {
throw new PlatformNotSupportedException(SR.GetString(SR.Method_Not_Supported_By_Iis_Integrated_Mode, "DefaultAuthentication.Authenticate"));
}
_eventHandler += value;
}
remove {
_eventHandler -= value;
}
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void Dispose() {
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public void Init(HttpApplication app) {
// adding this module back to IIS7
// it needs to run between Windows auth in PostAuthn
// and RoleManager (or anyone else who needs the principal)
// so ordering is important
// If the subscribed event changes, WindowsAuthenticationModule
// needs work, too.
if (HttpRuntime.UseIntegratedPipeline) {
app.PostAuthenticateRequest += new EventHandler(this.OnEnter);
}
else {
app.DefaultAuthentication += new EventHandler(this.OnEnter);
}
}
////////////////////////////////////////////////////////////
// OnAuthenticate: Custom Authentication modules can override
// this method to create a custom IPrincipal object from
// a DefaultIdentity
void OnAuthenticate(DefaultAuthenticationEventArgs e) {
////////////////////////////////////////////////////////////
// If there are event handlers, invoke the handlers
if (_eventHandler != null) {
_eventHandler(this, e);
}
}
////////////////////////////////////////////////////////////
// AddOnAuthenticate and RemoveOnAuthenticate: Use these
// methods to hook up event handlers to handle the
// OnAuthenticate Event
[SecurityPermission(SecurityAction.Assert, ControlPrincipal = true)]
void OnEnter(Object source, EventArgs eventArgs) {
HttpApplication app;
HttpContext context;
app = (HttpApplication)source;
context = app.Context;
////////////////////////////////////////////////////////////
// Step 1: Check if authentication failed
if (context.Response.StatusCode > 200) { // Invalid credentials
if (context.Response.StatusCode == 401)
WriteErrorMessage(context);
app.CompleteRequest();
return;
}
////////////////////////////////////////////////////////////
// Step 2: If no auth module has created an IPrincipal, then fire
// OnAuthentication event
if (context.User == null) {
OnAuthenticate (new DefaultAuthenticationEventArgs(context) );
if (context.Response.StatusCode > 200) { // Invalid credentials
if (context.Response.StatusCode == 401)
WriteErrorMessage(context);
app.CompleteRequest();
return;
}
}
////////////////////////////////////////////////////////////
// Step 3: Attach an anonymous user to this request, if none
// of the authentication modules created a user
if (context.User == null) {
context.SetPrincipalNoDemand(new GenericPrincipal(new GenericIdentity(String.Empty, String.Empty), new String[0]), false /*needToSetNativePrincipal*/);
}
Thread.CurrentPrincipal = context.User;
}
/////////////////////////////////////////////////////////////////////////////
void WriteErrorMessage(HttpContext context) {
context.Response.Write(AuthFailedErrorFormatter.GetErrorText());
// In Integrated pipeline, ask for handler headers to be generated. This would be unnecessary
// if we just threw an access denied exception, and used the standard error mechanism
context.Response.GenerateResponseHeadersForHandler();
}
}
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
internal class AuthFailedErrorFormatter : ErrorFormatter {
private static string _strErrorText;
private static object _syncObject = new object();
internal AuthFailedErrorFormatter() {
}
internal /*public*/ static string GetErrorText() {
if (_strErrorText != null)
return _strErrorText;
lock(_syncObject) {
if (_strErrorText == null)
_strErrorText = (new AuthFailedErrorFormatter()).GetErrorMessage();
}
return _strErrorText;
}
protected override string ErrorTitle {
get { return SR.GetString(SR.Assess_Denied_Title);}
}
protected override string Description {
get {
return SR.GetString(SR.Assess_Denied_Description1);
//"An error occurred while accessing the resources required to serve this request. This typically happens when you provide the wrong user-name and/or password.";
}
}
protected override string MiscSectionTitle {
get { return SR.GetString(SR.Assess_Denied_MiscTitle1);}
//"Error message 401.1";}
}
protected override string MiscSectionContent {
get {
string miscContent = SR.GetString(SR.Assess_Denied_MiscContent1);
AdaptiveMiscContent.Add(miscContent);
return miscContent;
//return "Logon credentials provided were not recognized. Make sure you are providing the correct user-name and password. Otherwise, ask the web server's administrator for help.";
}
}
protected override string ColoredSquareTitle {
get { return null;}
}
protected override string ColoredSquareContent {
get { return null;}
}
protected override bool ShowSourceFileInfo {
get { return false;}
}
}
}
|