1

我有一个Forms Authenticated Web应用程序,但是我需要基本身份验证,这些服务都位于特定路径(即“〜/ Services /” )。表单在特定文件夹/路径上使用基本身份验证验证的Web应用程序

我最初试图在web.config有一个单独定制的MembershipProvider添加标签的路径,像这样:

<location path="Services"> 
    <system.web> 
     <authentication mode="None" /> 
     <authorization> 
     <deny users="?" /> 
     </authorization> 
     <membership defaultProvider="ServicesMembershipProvider"> 
     <providers> 
      <add name="DefaultMembershipProvider" type="Company.WebProject.DeviceMembershipProvider" connectionStringName="DefaultConnectionString" applicationName="/" /> 
     </providers> 
     </membership> 
     <httpModules> 
     <add name="BasicAuthentication" type="Company.WebProject.BasicAuthenticationModule" /> 
     </httpModules> 
    </system.web> 
    </location> 

但是,这是引发错误:

这是一个错误在应用程序级别之外使用注册为allowDefinition ='MachineToApplication'的部分。此错误可能是由于虚拟目录未被配置为IIS中的应用程序。

因此,我意识到我不允许在location元素中使用身份验证元素。

在阅读this文章后,我尝试连接Global.asax中的FormsAuthentication_OnAuthenticate方法。由于我需要使用基本身份验证,因此我尝试返回401以提示浏览器提供基本身份验证凭据。不幸的是,这似乎是导致重定向到页面上的表单身份验证日志(即loginUrl)。

public void FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs e) 
{ 
    string path = VirtualPathUtility.ToAppRelative(e.Context.Request.Path); 
    if (path.Contains("/Services/")) 
    { 
     e.Context.Response.StatusCode = 401; 
     e.Context.Response.AddHeader("WWW-Authenticate", string.Format("Basic realm=\"{0}\"", "CompanyRealm")); 
     e.Context.Response.End();     
    } 
} 

所以现在我已经为如何在通过身份验证的Web应用程序窗体文件夹上实现基本验证江郎才尽。

有没有人有任何想法如何实现这一目标?

回答

0

您不能在ASP.NET中混合使用Forms AuthenticationWindows Authentication。您将需要为两者创建一个单独的应用程序,或者您需要实施Forms AuthenticationRoles才能正确执行分层访问。

+0

感谢您的回应,虽然如果你谷歌它,你会发现网上有几个解决方案混合形式认证和Windows身份验证。不幸的是,我需要Forms Auth和Basic Auth(而不是Windows Auth)。我知道我可以创建单独的应用程序,但这不是我理想的解决方案。 – 2012-07-19 16:01:41

0

有点晚了一天,但我已经张贴在这里的一些代码: Combining Forms Authentication and Basic Authentication

基本上你只需要

e.Context.Response.Flush(); 
e.Context.Response.Close(); 

更换

e.Context.Response.End(); 

关闭响应对象似乎阻止ASP覆盖重定向。请参阅上面的链接了解完整的代码。

相关问题