简体   繁体   English

ASP.NET 混合形式/Windows 身份验证

[英]ASP.NET Mixed Forms/Windows Authentication

I'm currently developing a MVC3 web application that needs to require extranet users to log in and be authenticated using Forms Authentication.我目前正在开发一个 MVC3 web 应用程序,该应用程序需要要求外网用户登录并使用 Forms 身份验证进行身份验证。 Intranet users should be logged in automatically using Windows authentication.内网用户应使用 Windows 身份验证自动登录。
I've found this article, http://aspalliance.com/553_Mixed_Mode_Authentication.all but it's dated Nov 2004 and like to find something written more recently than 7 years ago.我找到了这篇文章http://aspalliance.com/553_Mixed_Mode_Authentication.all但它的日期是 2004 年 11 月,我想找到比 7 年前更近的东西。

My plan is to have two applications in IIS, with virtual directories pointing at the same physical directory, but one will allow Anonymous Access and the other will not.我的计划是在 IIS 中有两个应用程序,虚拟目录指向同一个物理目录,但一个允许匿名访问,另一个不允许。

When a user is authenticated on the Windows/Intranet side of things, I hope to simply simulate the user logging in via forms authentication.当用户在 Windows/Intranet 端进行身份验证时,我希望简单地模拟用户通过 forms 身份验证登录。 Are there any pitfalls to this approach?这种方法有什么陷阱吗? Any better ideas?有更好的想法吗?

EDIT: 7/22/2011编辑:2011 年 7 月 22 日

I'm using IIS7 which won't allow me to do many of the things suggested in the older articles.我正在使用 IIS7,它不允许我做旧文章中建议的许多事情。 Due to authentication being integrated a bit tighter between IIS7 and the ASP.NET web sites, certain things aren't allowed.由于身份验证在 IIS7 和 ASP.NET web 站点之间集成得更紧密,因此不允许某些事情。 For example, I can't set Windows Auth on a single file while the rest of the application is using Forms Auth.例如,当应用程序的 rest 使用 Forms Auth 时,我无法在单个文件上设置 Windows Auth。

Wondering if the best approach here wouldn't be to have two applications where the first application uses windows authentication and consist solely of a hook to the PostAuthenticate event in the HTTP pipeline.想知道这里的最佳方法是否不是有两个应用程序,其中第一个应用程序使用 windows 身份验证,并且仅包含 HTTP 管道中的 PostAuthenticate 事件的挂钩。 If the user is authenticated, you give them a forms ticket and redirect to the target app, App2, which uses forms authentication.如果用户通过了身份验证,则给他们一个 forms 票证并重定向到使用 forms 身份验证的目标应用程序 App2。 You have to be careful that the cookies are not path specific and also that the two apps reside on the same server (or that the encryption keys are synchronized in web.config).您必须注意 cookies 不是特定于路径的,并且两个应用程序驻留在同一服务器上(或者加密密钥在 web.config 中同步)。 If the user is not authenticated, you simply redirect them without a an auth ticket and they login when the arrive at App2.如果用户未通过身份验证,您只需在没有身份验证票的情况下重定向他们,然后他们在到达 App2 时登录。

App1: www.myUrl.com\MyApp应用程序1:www.myUrl.com\MyApp

This is the "public" url for the app and detects network users by hooking into the PostAuthenticate event (see Professional ASP.NET 2.0 Security, Membership, and Role Management ):这是应用程序的“公共”url,并通过挂钩 PostAuthenticate 事件来检测网络用户(请参阅Professional ASP.NET 2.0 安全、成员资格和角色管理):

//Hook PostAuthenticateRequest inside of global.asax
void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
    IPrincipal p = HttpContext.Current.User;

    if (p.Identity.IsAuthenticated)
    {
      // to do: give them a non-path specific ticket and redirect to App2
    }
}

App2: www.myUrl.com\MyApp2应用程序2:www.myUrl.com\MyApp2

This is the actual application.这是实际应用。 When network users arrive from App1, they'll already have a forms ticket.当网络用户从 App1 到达时,他们已经有一张 forms 票。 When non-network users arrive, they'll be redirected to login.aspx.当非网络用户到达时,他们将被重定向到 login.aspx。

Notes: One downside of this would be if network users bookmark App2.注意:这样做的一个缺点是网络用户将 App2 加入书签。 I'm not quite sure how I would get around this.我不太确定如何解决这个问题。 If they have a non-expiring cookie, it wouldn't matter too much.如果他们有一个不会过期的 cookie,那也没关系。 One option would be to put a link on the login page that says something like "I'm already a network user - log me in automatically", which would link back to App1, where they would get logged in?一种选择是在登录页面上放置一个链接,上面写着“我已经是网络用户 - 自动登录”,这将链接回 App1,他们将在哪里登录?

I have some code to assist with issuing a forms ticket.我有一些代码可以帮助签发 forms 票证。 I'll update the answer as I have time.我有时间会更新答案。

Note that you're going to have to do some fancy role-management footwork in App2 to handle the disparate role providers.请注意,您将不得不在 App2 中做一些花哨的角色管理步骤来处理不同的角色提供者。 That Amazon reference above is old, but I find myself constantly referencing it when I run into these kinds of custom Authentication and Authorization problems.上面提到的亚马逊参考是旧的,但是当我遇到这些自定义身份验证和授权问题时,我发现自己不断地参考它。

This is perfectly possible, if You will create two different apps in IIS, so you job is done!这是完全可能的,如果您将在 IIS 中创建两个不同的应用程序,那么您的工作就完成了! =) =)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM