在 dll 中使用 ASP.NET 会员资格提供程序

发布于 2024-10-09 18:36:09 字数 644 浏览 1 评论 0原文

过去几年我在网络应用程序中使用了会员提供程序。我现在对工作中的内部项目有一个新的“请求”。他们希望有一个服务(而不是 Web 服务)来进行快速身份验证。基本上,公开 ValidateUser(UserName, Password) 方法...

我正在将其构建在一个 DLL 中,该 DLL 将与我们的内部网站一起使用。完成这项工作的最佳方法是什么?该 DLL 将不会引用 Web 应用程序,而 Web 应用程序引用该 DLL。如何让 DLL 知道成员资格提供程序?

TIA

PS:如果这个问题已在其他地方得到解答,请指导我...

编辑:我找到了一篇有关在 WinForms 和/或 WPF 应用程序中使用 ASP.NET 会员资格的文章。不幸的是,这些依赖于 app.config 文件。 DLL 似乎在发布后不再使用 app.config。如果我错了,请纠正我!文章位于:http://aspalliance.com/1595_Client_Application_Services__Part_1.all

I've used Membership Providers in web apps over the last several years. I now have a new "request" for an internal project at work. They would like a service (not a web service) to do a quick authenticate against. Basically, exposing the ValidateUser(UserName, Password) method...

I am building this in a DLL that will sit with our internal web site. What is the best approach to make this work? The DLL will not reference the web app and the web app will reference the DLL. How do I make the DLL aware of the Membership Provider?

TIA

PS: If this has been answered elsewhere please direct me to that...

EDIT: I found an article on using ASP.NET Membership with WinForms and/or WPF applications. Unfortunately, these depend on an app.config file. A DLL appears to not use the app.config once published. If I am wrong, please set me straight! The article is here: http://aspalliance.com/1595_Client_Application_Services__Part_1.all

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

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

发布评论

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

评论(2

小傻瓜 2024-10-16 18:36:09

好吧,看来设置 SqlMembershipProvider 连接字符串的唯一方法是使用 初始化方法,根据文档,不应从我们的代码中调用该方法。

初始化 SQL Server 成员资格
具有属性值的提供者
在 ASP.NET 应用程序中指定
配置文件。这个方法不是
旨在直接从您的
代码。

因此,基本上我们必须找到一种方法将该信息放入与托管 dll 的应用程序关联的配置文件中。

您所要做的就是在程序集中引用 system.web.dllsystem.configuration.dll,使用 SqlMembershipProvider,然后在应用程序中设置正确的值。可执行文件的配置如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="MembershipConnectionString" connectionString="connectionstringdetails" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <membership defaultProvider="DefaultSqlMembershipProvider">
      <providers>
        <clear />
        <add name="DefaultSqlMembershipProvider" connectionStringName="MembershipConnectionString" type="System.Web.Security.SqlMembershipProvider" />
      </providers>
    </membership>
  </system.web>
</configuration>

需要注意的重要一点是,如果您“MyAssembly.dll”和“TheApp.exe”,则应该是“TheApp.exe.config”,而不是“MyAssembly.dll.config”。配置文件始终与正在执行的程序集关联。

Well, it appears that the only way to set the connection string of a SqlMembershipProvider is to use the Initialize method, which according to the documentation, shouldn't be called from our code.

Initializes the SQL Server membership
provider with the property values
specified in the ASP.NET application's
configuration file. This method is not
intended to be used directly from your
code.

So basically we have to figure out a way to get that information into a config file associated with the application that's hosting the dll.

All you have to do is reference system.web.dll and system.configuration.dll in your assembly, use the SqlMembershipProvider, and then set the proper values in the app.config for executable like so:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="MembershipConnectionString" connectionString="connectionstringdetails" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <membership defaultProvider="DefaultSqlMembershipProvider">
      <providers>
        <clear />
        <add name="DefaultSqlMembershipProvider" connectionStringName="MembershipConnectionString" type="System.Web.Security.SqlMembershipProvider" />
      </providers>
    </membership>
  </system.web>
</configuration>

The important thing to note is that if you "MyAssembly.dll" and "TheApp.exe", this should be "TheApp.exe.config", not "MyAssembly.dll.config". The config file is always associated with the executing assembly.

神经大条 2024-10-16 18:36:09

在网站中,当应用程序启动时(global.asax 中的Application_Start),您可以将网站识别的成员资格提供程序的引用传递给您编写的DLL。该 dll 只需要 System.Web.Security.MembershipProvider 类型。这样,dll 仍然可以从实现中调用所需的方法,但在编译时不知道提供程序的运行时类型(这就是所谓的依赖注入)。

In the website, when the application starts (Application_Start in the global.asax), you can pass a reference of the membership provider that the website is aware of to the DLL you've written. The dll will simply expect the type System.Web.Security.MembershipProvider. This way the dll can still invoke the needed method from the implementation, but not know the runtime type of the provider at compile time (this is what is known as Dependency Injection).

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