<%@ Application Language="C#" %>
<%@ Import Namespace="System.Security.Principal" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.Security" %>
<script runat="server">
public void FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs e)
{
if (null == e) throw new ArgumentNullException("e");
// Check that we have a Windows user
WindowsIdentity winUser = e.Context.Request.LogonUserIdentity;
if (null == winUser) return;
// Check that the path allows Windows authentication
string path = VirtualPathUtility.ToAppRelative(e.Context.Request.Path);
if (!IsWindowsAuthenticated(path, e.Context)) return;
// Don't allow guest accounts to access
if (winUser.IsAnonymous || winUser.IsGuest || winUser.IsSystem) return;
WindowsPrincipal winPrincipal = new WindowsPrincipal(winUser);
if (winPrincipal.IsInRole("Guests")) return;
e.User = winPrincipal;
}
private static bool IsWindowsAuthenticated(string path, HttpContext context)
{
if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path");
if (null == context) throw new ArgumentNullException("context");
// Check that the path starts with the restricted folder:
if (path.StartsWith("/restricted/", StringComparison.OrdinalIgnoreCase)) return true;
return false;
}
</script>