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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
//------------------------------------------------------------------------------
// <copyright file="PassportAuthenticationModule.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
/*
* PassportAuthenticationModule class
*
* Copyright (c) 1999 Microsoft Corporation
*/
namespace System.Web.Security {
using System.Web;
using System.Security.Principal;
using System.Web.Configuration;
using System.Web.Handlers;
using System.Globalization;
using System.Security.Permissions;
using System.Web.Management;
/// <devdoc>
/// This
/// module provides a wrapper around passport authentication services.
/// </devdoc>
[Obsolete("This type is obsolete. The Passport authentication product is no longer supported and has been superseded by Live ID.")]
public sealed class PassportAuthenticationModule : IHttpModule {
private PassportAuthenticationEventHandler _eventHandler;
private static bool _fAuthChecked = false;
private static bool _fAuthRequired = false;
private static String _LoginUrl = null;
/// <devdoc>
/// <para>
/// Initializes a new instance of the <see cref='System.Web.Security.PassportAuthenticationModule'/>
/// class.
/// </para>
/// </devdoc>
[SecurityPermission(SecurityAction.Demand, Unrestricted=true)]
public PassportAuthenticationModule() {
}
////////////////////////////////////////////////////////////
// AddOnAuthenticate and RemoveOnAuthenticate: Use these
// methods to hook up event handlers to handle the
// OnAuthenticate Event
/// <devdoc>
/// This is a global.asax event that must be
/// named PassportAuthenticate_OnAuthenticate event.
/// </devdoc>
public event PassportAuthenticationEventHandler Authenticate {
add {
_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) {
app.AuthenticateRequest += new EventHandler(this.OnEnter);
app.EndRequest += new EventHandler(this.OnLeave);
}
////////////////////////////////////////////////////////////
// OnAuthenticate: Custom Authentication modules can override
// this method to create a custom IPrincipal object from
// a PassportIdentity
/// <devdoc>
/// Calls the
/// PassportAuthentication_OnAuthenticate handler, if one exists.
/// </devdoc>
void OnAuthenticate(PassportAuthenticationEventArgs e) {
////////////////////////////////////////////////////////////
// If there are event handlers, invoke the handlers
if (_eventHandler != null) {
_eventHandler(this, e);
if (e.Context.User == null && e.User != null)
{
InternalSecurityPermissions.ControlPrincipal.Demand();
e.Context.User = e.User;
}
}
////////////////////////////////////////////////////////////
// Default Implementation: If IPrincipal has not been created,
// create a PassportUser
if (e.Context.User == null)
{
InternalSecurityPermissions.ControlPrincipal.Demand();
e.Context.User = new PassportPrincipal(e.Identity, new String[0]);
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Methods for internal implementation
/// <internalonly/>
/// <devdoc>
/// </devdoc>
void OnEnter(Object source, EventArgs eventArgs) {
if (_fAuthChecked && !_fAuthRequired)
return;
HttpApplication app;
HttpContext context;
app = (HttpApplication)source;
context = app.Context;
if (!_fAuthChecked) {
AuthenticationSection settings = RuntimeConfig.GetAppConfig().Authentication;
_fAuthRequired = (AuthenticationConfig.Mode == AuthenticationMode.Passport);
_LoginUrl = settings.Passport.RedirectUrl;
_fAuthChecked = true;
}
if (!_fAuthRequired)
return;
////////////////////////////////////////////////////////
// Step 1: See if this request is valid or not
// VSWhidbey 442515: We no longer need to do this check, always proceed
////////////////////////////////////////////////////////
// Step 2: Create a Passport Identity from the credentials
// from IIS
PassportIdentity identity = new PassportIdentity();
////////////////////////////////////////////////////////
// Step 4: Call OnAuthenticate virtual method to create
// an IPrincipal for this request
OnAuthenticate( new PassportAuthenticationEventArgs(identity, context) );
////////////////////////////////////////////////////////
// Skip AuthZ if accessing the login page
context.SetSkipAuthorizationNoDemand(AuthenticationConfig.AccessingLoginPage(context, _LoginUrl), false /*managedOnly*/);
if (!context.SkipAuthorization) {
context.SkipAuthorization = AssemblyResourceLoader.IsValidWebResourceRequest(context);
}
}
void OnLeave(Object source, EventArgs eventArgs) {
HttpApplication app;
HttpContext context;
app = (HttpApplication)source;
context = app.Context;
if (!_fAuthChecked || !_fAuthRequired || context.User == null || context.User.Identity == null || !(context.User.Identity is PassportIdentity))
return;
PassportIdentity id = (PassportIdentity) context.User.Identity;
if (context.Response.StatusCode != 401 || id.WWWAuthHeaderSet)
return;
if ( _LoginUrl==null || _LoginUrl.Length < 1 || String.Compare(_LoginUrl, "internal", StringComparison.Ordinal) == 0) {
context.Response.Clear();
context.Response.StatusCode = 200;
if (!ErrorFormatter.RequiresAdaptiveErrorReporting(context)) {
String strUrl = context.Request.Url.ToString();
int iPos = strUrl.IndexOf('?');
if (iPos >= 0) {
strUrl = strUrl.Substring(0, iPos);
}
String strLogoTag = id.LogoTag2(HttpUtility.UrlEncode(strUrl, context.Request.ContentEncoding));
String strMsg = SR.GetString(SR.PassportAuthFailed, strLogoTag);
context.Response.Write(strMsg);
}
else {
ErrorFormatter errorFormatter = new PassportAuthFailedErrorFormatter();
context.Response.Write(errorFormatter.GetAdaptiveErrorMessage(context, true));
}
}
else {
////////////////////////////////////////////////////////////
// Step 1: Get the redirect url
String redirectUrl = AuthenticationConfig.GetCompleteLoginUrl(context, _LoginUrl);
////////////////////////////////////////////////////////////
// Step 2: Check if we have a valid url to the redirect-page
if (redirectUrl == null || redirectUrl.Length <= 0)
throw new HttpException(SR.GetString(SR.Invalid_Passport_Redirect_URL));
////////////////////////////////////////////////////////////
// Step 3: Construct the redirect-to url
String strUrl = context.Request.Url.ToString();
String strRedirect;
int iIndex;
String strSep;
if (redirectUrl.IndexOf('?') >= 0)
strSep = "&";
else
strSep = "?";
strRedirect = redirectUrl + strSep + "ReturnUrl=" + HttpUtility.UrlEncode(strUrl, context.Request.ContentEncoding);
////////////////////////////////////////////////////////////
// Step 4: Add the query-string from the current url
iIndex = strUrl.IndexOf('?');
if (iIndex >= 0 && iIndex < strUrl.Length-1)
strRedirect += "&" + strUrl.Substring(iIndex+1);
////////////////////////////////////////////////////////////
// Step 5: Do the redirect
context.Response.Redirect(strRedirect, false);
}
}
}
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
// ErrorFormatter for generating adaptive error for mobile devices
internal class PassportAuthFailedErrorFormatter : ErrorFormatter {
protected override string ErrorTitle {
get { return SR.GetString(SR.PassportAuthFailed_Title);}
}
protected override string Description {
get { return SR.GetString(SR.PassportAuthFailed_Description);}
}
protected override string MiscSectionTitle {
get { return SR.GetString(SR.Assess_Denied_Title);}
}
protected override string MiscSectionContent {
get { return null;}
}
protected override string ColoredSquareTitle {
get { return null;}
}
protected override string ColoredSquareContent {
get { return null;}
}
protected override bool ShowSourceFileInfo {
get { return false;}
}
}
}
|