简体   繁体   English

实时测试ASP.NET自定义角色提供程序

[英]Live testing ASP.NET custom Role Provider

Using ASP.NET Webforms + VB/C# 使用ASP.NET Webforms + VB / C#

I've been tasked to restrict ASP.NET page access to users not in specific roles. 我的任务是限制ASP.NET页面访问权限不属于特定角色的用户。 And I need to be able to live test my solution (versus unit testing where I could use mocks or fakes). 而且我需要能够对我的解决方案进行实时测试(与可以使用模拟或伪造品的单元测试相比)。 Our site is rather complex so I doubt I'd be able to find all the "gotchas" with just unit testing. 我们的网站相当复杂,因此我怀疑我仅通过单元测试就能找到所有的“陷阱”。

I have something on my development computer I'm pretty sure will work in Production: I have a custom Role Provider hooked into the web.config file. 我肯定在开发计算机上有东西,我肯定可以在生产环境中使用:我有一个自定义的角色提供程序,已连接到web.config文件中。 It's being initialized and called when I debug the website, so I'm reasonably sure it's working OK. 我在调试网站时正在初始化和调用它,因此我可以肯定地确定它可以正常工作。 I have a folder ("Administration") marked only for a specific role. 我有一个仅标记了特定角色的文件夹(“管理”)。 Our roles are defined in our own database and are not tied to Microsoft or Windows roles/permissions. 我们的角色是在我们自己的数据库中定义的,与Microsoft或Windows的角色/权限无关。

The problem is: I cannot actually login as a user I wish to debug with. 问题是:我实际上无法以要调试的用户身份登录。 I can "simulate" this with a special development-only start page. 我可以使用特殊的仅用于开发的起始页来“模拟”此操作。 That works OK for our menu/navigation items which are built from the roles database, but of course without restricting pages (with a Role Provider or something similar), you can still manually type in a page and it will be served up. 对于从角色数据库构建的菜单/导航项,这行之有效,但是当然不限制页面(使用“角色提供程序”或类似功能),您仍然可以手动键入页面并将其提供。 This devel-only start page sets the username I pass in as a FormsAuthentation auth cookie. 这个仅开发的起始页面将我传递的用户名设置为FormsAuthentation身份验证cookie。

FormsAuthentication.Initialize()
FormsAuthentication.SignOut()
FormsAuthentication.SetAuthCookie(simUserName, True)

It seems like when I first start debugging (usually after a reboot), the custom Role Provider will get called with the same username I'm simulating, but after a while, that suddenly stops and my local Windows name gets passed instead. 好像当我第一次开始调试时(通常是在重新启动后),将使用与我模拟的用户名相同的用户名调用自定义角色提供程序,但过一会儿,该程序突然停止并且改用了我的本地Windows名称。 (Cookie issue?) After that, it no longer works. (Cookie问题?)之后,它不再起作用。

Anyway -- Is there a way I can test roles locally during development or will we just have to put this on Production and hope for the best. 无论如何-有没有一种方法可以在开发过程中本地测试角色,还是只需要将其投入生产并希望达到最佳状态。

I didn't know what code or settings would be useful at this time, so let me know what you need. 我目前不知道哪些代码或设置会有用,所以请告诉我您需要什么。 Thanks! 谢谢!

I think I've found the solution to my problem. 我想我已经找到解决问题的方法。 I decided to work with some of the samples in Ch. 我决定使用Ch中的一些样本。 7 of Beginning ASP.NET Security by Barry Dorrans (published by Wrox) in the hopes working with authentication and authorization in simple examples might lead to a solution, and it did. Barry Dorrans(由Wrox出版)的《 Beginning ASP.NET Security》的第7册希望通过简单的示例使用身份验证和授权可能会导致解决方案,但确实如此。

One of the examples of using Forms authentication (pp. 155-157) showed a simple login.aspx page similar to my development startup page. 使用表单身份验证的示例之一(第155-157页)显示了一个简单的login.aspx页面,类似于我的开发启动页面。 The code I had in my Development startup page (shown above) was incorrect. 我在开发启动页面(如上所示)中的代码不正确。 It should have been: 应该是:

FormsAuthentication.Authenticate(simUserName, simUserPassword)
FormsAuthentication.RedirectFromLoginPage(simUserName, False)

I also defined my simulated users in my Development web.config: 我还在开发web.config中定义了模拟用户:

<authentication mode="Forms">
  <forms defaultUrl="default.aspx" loginUrl="mystart.aspx">
    <credentials passwordFormat="Clear">
      <user name="GeorgeWashington" password="password"/>
      <user name="AndrewJackson" password="password"/>
      <user name="AbrahamLincoln" password="password"/>
      <user name="TeddyRoosevelt" password="password"/>
      <user name="JackKennedy" password="password"/>
    </credentials>
  </forms>
</authentication>

When FormsAuthentication.Authenticate is called and authenticates which ever of the simulated users I choose, it looks like it causes ASP.NET to now always use this username in calls to the Role Provider. 调用FormsAuthentication.Authenticate并对我选择的每个模拟用户进行身份验证时,似乎导致ASP.NET现在始终在对角色提供程序的调用中使用此用户名。 At least, this appears to be working when debugging the website. 至少,这在调试网站时似乎有效。

I've set the project to always call my Development startup page (the login page -- mystart.aspx). 我将项目设置为始终调用我的开发启动页面(登录页面-mystart.aspx)。 This way I always start with a fresh authentication in case I need to work with a different role. 这样,在需要使用其他角色的情况下,我总是从全新的身份验证开始。

Anyone wanting to use this solution, WARNING : Your Development startup page must NEVER be used for the Production website. 任何想要使用此解决方案的人, 警告切勿将“开发”启动页面用于生产网站。 Likewise, NEVER use usernames and passwords in a Production web.config. 同样, 切勿在Production web.config中使用用户名和密码。 This is ONLY for debugging and testing. 用于调试和测试。 Depending how you version control your development and production code, you may have to manually merge certain changes going from development to production in order to avoid sending this debugging code to Production. 根据版本控制开发和生产代码的方式,可能必须手动合并从开发到生产的某些更改,以避免将此调试代码发送到生产。

I also understand Microsoft wants us to use a Membership Provider in place of the FormsAuthentication class. 我也了解Microsoft希望我们使用Membership Provider代替FormsAuthentication类。 And in Production code, we should. 在生产代码中,我们应该这样做。 But for my purposes (ensuring I can interact with the website in differing users/roles while debugging), this appears to be the simplest solution. 但是出于我的目的(确保在调试时可以与不同的用户/角色进行网站交互),这似乎是最简单的解决方案。

EDIT -- one more piece of the puzzle: I was still getting strange method calls in my Custom Role Provider. 编辑 -还有一个难题:我的自定义角色提供程序中仍然收到奇怪的方法调用。 A few times a method would be called with the username I logged in with on my login page (this is what I expected); 几次会使用我在登录页面上登录时使用的用户名调用方法(这是我所期望的); most often it was my Windows username which led me to believe Windows authorization was still being used by ASP.NET somewhere . 经常是我的Windows用户名,害得我相信仍然被使用ASP.NET 某处的Windows授权。 After more research (thanks SO!!!), I found in my applicationhost.config file for my project, Windows authentication was set to True which I guess was causing a conflict with my web.config file. 经过更多研究(谢谢!!!!),我在我的项目的applicationhost.config文件中发现Windows身份验证设置为True,我认为这与web.config文件引起了冲突。 Setting the values in applicationhost.config to: 将applicationhost.config中的值设置为:

<location path="MyWebSite">
    <system.webServer>
        <security>
            <authentication>
                <anonymousAuthentication enabled="true" />
                <windowsAuthentication enabled="false" />
            </authentication>
        </security>
    </system.webServer>
</location>

seems to have solved that issue. 似乎已经解决了这个问题。

If you have any questions, or need more information, please let me know. 如果您有任何疑问,或需要更多信息,请告诉我。

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

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