供应商模式与 默认提供者

发布于 2024-07-14 09:39:51 字数 669 浏览 6 评论 0原文

我正在尝试在自定义 Web 控件中实现提供者模式。 我仍在尝试理解该模式,并且有以下问题。

默认提供程序是我的控件加载时始终使用的提供程序吗? 据我所知,使用的提供程序将始终是默认的,但我不确定,因为 MSDN 文档说允许选择提供程序的服务器控件应该具有 Provider 属性,该属性应默认为 defaultProvider 的值。 对我来说,这意味着控件可以根据其 Provider 属性加载给定的提供程序,只要该提供程序位于配置文件中即可。 那是对的吗? 因此,如果没有请求特定提供程序,则返回默认提供程序。

要更改提供程序,我是否只需更改配置文件中的默认提供程序? <-- 我认为这是不正确的,因为这只会更改返回的 defaultProvider。

这是正确的:在控件中,defaultprovider 将在控件的 OnLoad 事件方法中加载吗? 我需要根据提供者传递来自不同源的控制数据,但结果数据将被放入一个类中。 控件将用来呈现自身。

我也很困惑如何从页面获取数据到控件? 假设我的 ProviderBase 有一个名为 LoadData 的方法

,那么在我的 SQLCustomerProvider 中,我将实现 LoadData 方法,是否可以在此处自定义此代码以读取我的特定数据并加载到类中,或者应该使用控件的页面将数据加载到类中?

谢谢!

I am trying to implement the provider pattern in a custom web control. I am still trying to understand the pattern and I have the following questions.

Is the default provider the provider that is always going to be used when my control loads? From what I can tell the provider used will always be the default but I am not sure because the MSDN documentation says that server controls that allow selection of Providers should have Provider property that should default to the value of the defaultProvider. To me that means the control can load a a given provider based on it's Provider property as long as that provider is in the Config file. Is that correct? So the default provider is returned if no specific provider is requested.

To change providers do I just simply change the defaultprovider in my config file? <-- I think that is incorrect as that would just change the defaultProvider returned.

Is this correct: In a control then the defaultprovider would be loaded in the OnLoad event method of the control?
I need to pass the control data from different sources depending on the provider but the resulting data would then be put into a class. that the control would use to render itself.

I am also confused how to get the data to the control from the page?
Say my ProviderBase had a method called LoadData

Then in my SQLCustomerProvider I would implement the LoadData method, is it ok to customize this code here to read my specific data and load into the class or should the page using the control load the data into the class?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一世旳自豪 2024-07-21 09:39:52

我首先承认,我对提供程序的了解仅限于为成员资格/个人资料/角色编写它们,但一般内容仍应适用:

除非您允许开发人员有机会指定不同的提供程序,否则是的,默认提供程序是要使用的那个。

因此,以身份验证为例,如果您的 web.config 中有以下内容:

<membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="20">
  <providers>
    <remove name="AspNetSqlProvider" />
    <add name="SqlProvider"
      type="System.Web.Security.SqlMembershipProvider"
      connectionStringName="SqlServices"
      enablePasswordRetrieval="false"
      enablePasswordReset="true"
      requiresQuestionAndAnswer="true"
      passwordFormat="Hashed"
      applicationName="/" />
    <add name="AdProvider"
      type="System.Web.Security.ActiveDirectoryMembershipProvider" />
  </providers>
</membership>

当您在页面上的某处添加登录控件时,那么将使用 SqlProvider。

您可以通过以下两种方式之一使用不同的提供程序:

  1. 您可以使用 MembershipProvider 登录控件的属性
  2. 您可以提供要在登录控件上调用的方法身份验证 事件,您可以在其中选择您的提供商。

方法 2 的一个示例是:

private void OnAuthenticate(object sender, AuthenticateEventArgs e){
  bool authenticated = false;

  MembershipProvider sqlProvider = Membership.Providers["SqlProvider"];

  authenticated = sqlProvider.ValidateUser(Login1.UserName, Login1.Password);

  if (!authenticated){
    // User not found in database, try Active Directory:
    MembershipProvider adProvider = Membership.Providers["AdProvider"];
    authenticated = adProvider.ValidateUser(Login1.UserName, Login1.Password);
  }

  e.Authenticated = authenticated;
}

您可以在自己的控件上执行此操作的其他方法是公开 Provider 属性,并检查该属性是否具有值,然后使用该提供程序而不是 defaultProvider。

通常,提供程序模型已用于向已知类提供一组默认行为 - 因此成员资格提供程序都具有(例如)方法 GetUser 返回 MembershipUser - AspNetSqlMembershipProvider 实现 GetUser 的全部目的是从 ASP.NET SQL 数据库加载 MembershipUser 数据 - 您可以在 示例会员提供程序

我希望有所帮助!

I'll admit up front that my knowledge of providers is limited to writing them for Membership/Profile/Roles, but the general stuff should still apply:

Unless you allow the developer the oppertunity to specify a different provider, then yes, the default provider is the one that is going to be used.

So, taking authentication as an example, if you had the following in your web.config:

<membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="20">
  <providers>
    <remove name="AspNetSqlProvider" />
    <add name="SqlProvider"
      type="System.Web.Security.SqlMembershipProvider"
      connectionStringName="SqlServices"
      enablePasswordRetrieval="false"
      enablePasswordReset="true"
      requiresQuestionAndAnswer="true"
      passwordFormat="Hashed"
      applicationName="/" />
    <add name="AdProvider"
      type="System.Web.Security.ActiveDirectoryMembershipProvider" />
  </providers>
</membership>

When you add a Login control somewhere on a page, then that will use SqlProvider.

You can use a different provider in one of two ways:

  1. You can specify a different provider using MembershipProvider property of the Login control
  2. You could supply a method to be called on the Login controls Authenticate event, where you can pick your provider.

An example of method 2 is:

private void OnAuthenticate(object sender, AuthenticateEventArgs e){
  bool authenticated = false;

  MembershipProvider sqlProvider = Membership.Providers["SqlProvider"];

  authenticated = sqlProvider.ValidateUser(Login1.UserName, Login1.Password);

  if (!authenticated){
    // User not found in database, try Active Directory:
    MembershipProvider adProvider = Membership.Providers["AdProvider"];
    authenticated = adProvider.ValidateUser(Login1.UserName, Login1.Password);
  }

  e.Authenticated = authenticated;
}

Other ways you could do this on your own control is to expose a Provider property, and check to see if that has a value, and use that provider instead of the defaultProvider.

Generally, the provider model has been used to provide a default set of behaviour to a known class - so the Membership providers all have (for example) a method GetUser that return a MembershipUser - the whole purpose of the AspNetSqlMembershipProvider's implementation of GetUser is to load the MembershipUser data from the ASP.NET SQL database - you can see this in action in the Sample Membership Provider

I hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文