简体   繁体   English

如何在C#类(.cs文件)和ASP.NET .aspx文件中使用相同的OracleMembershipProvider?

[英]How can you use the same OracleMembershipProvider in C# classes (.cs files) AND ASP.NET .aspx files?

Here's the problem, I want to handle users using the built in Membership functionality, specifically using Oracle.Web.Security.OracleMembershipProvider. 这是问题所在,我想使用内置的Membership功能(特别是使用Oracle.Web.Security.OracleMembershipProvider)来处理用户。 I've got the provider to work in .aspx files, correctly inserting new users into the appropriate Oracle tables and handling logins. 我已经使提供程序可以在.aspx文件中工作,可以将新用户正确插入适当的Oracle表中并处理登录。 However, I have a webservice beside the website component of the solution. 但是,我在解决方案的网站组件旁边有一个Web服务。 I want only users who registered via the website and that can send a valid username/password with web-requests to be able to access data. 我只希望那些通过网站注册并且可以发送带有网络请求的有效用户名/密码的用户能够访问数据。

Anyways, in a .cs file I do this: 无论如何,在.cs文件中,我这样做:

OracleMembershipProvider provider = new OracleMembershipProvider(); 
if (!provider.ValidateUser(username, password)) 
{
    return null;
}

The call to ValidateUser throws an exception, details: "OracleConnection.ConnectionString is invalid". 对ValidateUser的调用将引发异常,详细信息:“ OracleConnection.ConnectionString无效”。

I'd like to think the fact that the connection string works on the .aspx pages is enough to confirm the connectionstring is valid. 我想认为连接字符串在.aspx页上有效这一事实足以确认连接字符串有效。

Here's what it looks like in the .aspx file, the key being on the second line MembershipProvider="MyOracleMembershipProvider" 这是.aspx文件的外观,键位于第二行MembershipProvider =“ MyOracleMembershipProvider”

<asp:Login ID="LoginUser" runat="server" EnableViewState="false" 
    RenderOuterTable="false" MembershipProvider="MyOracleMembershipProvider">
    <LayoutTemplate>
        <span class="failureNotification">
            <asp:Literal ID="FailureText" runat="server"></asp:Literal>
        </span>
        <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification" 
             ValidationGroup="LoginUserValidationGroup"/>
        <div class="accountInfo">
            <fieldset class="login">
                <legend>Account Information</legend>
                <p>
                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label>
                    <asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
                         CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." 
                         ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                </p>
                //blah blah blah, code removed for length.
            </fieldset>
            <p class="submitButton">
                <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="LoginUserValidationGroup"/>
            </p>
        </div>
    </LayoutTemplate>
</asp:Login>

Viewing some of the details of 'provider' reveal basically all null/default values, indicating it wasn't initialized properly. 查看“提供者”的一些详细信息,可以发现基本上所有的空值/默认值,表明它没有正确初始化。 I'm under the impression the constructor should initialize the membership provider using my default membership provider, as per my web.config file, which is as follows: 我的印象是,构造函数应根据我的web.config文件使用默认的成员资格提供程序初始化成员资格提供程序,如下所示:

<configuration>
  <system.data>
    <DbProviderFactories>      
      <clear />
      <add name="xxxOracleDataProvider" invariant="Oracle.DataAccess.Client" description=".Net Framework Data Provider for Oracle" 
       type="Oracle.DataAccess.Client.OracleClientFactory, 
             Oracle.DataAccess, 
             Version=4.112.2.0, 
             Culture=neutral, 
             PublicKeyToken=89b483f429c47342" />
</DbProviderFactories>
</system.data>  

<connectionStrings>
  <add name="MEMBERSHIP_PROVIDER" 
     connectionString="
      Persist Security Info=True;
      user id = xxxxxx; 
      password = xxxxxx; 
      data source=
      (DESCRIPTION = 
        (ADDRESS_LIST = 
          (ADDRESS = 
            (PROTOCOL = TCP)
            (HOST = xxxxxxx)
            (PORT = xxxxxxxx)
          )
        )
        (CONNECT_DATA = 
         (SERVICE_NAME = xxxxx)
         (SERVER = DEDICATED)
        )
      );"
     providerName="Oracle.DataAccess"
     />       
</connectionStrings>

<system.web>
<customErrors mode="Off"/>
<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>
<compilation debug="true"/>    
<authentication mode="Forms">
  <forms loginUrl="~/Account/Login.aspx" timeout="2880"/>
</authentication>
<membership defaultProvider="MyOracleMembershipProvider" hashAlgorithmType="SHA1">
  <providers>
    <clear />        
    <add name="MyOracleMembershipProvider"
         type="Oracle.Web.Security.OracleMembershipProvider, 
         Oracle.Web, Version=4.112.1.2, Culture=neutral,
         PublicKeyToken=89b483f429c47342"
         connectionStringName="MEMBERSHIP_PROVIDER"
         applicationName="xxx_xxxx_xxxxxx"
         enablePasswordRetrieval="false"
         enablePasswordReset="true"
         requiresQuestionAndAnswer="false"
         requiresUniqueEmail="true"
         passwordFormat="Hashed"
         minRequiredNonalphanumericCharacters="0"
         maxInvalidPasswordAttempts="7"
         minRequiredPasswordLength="12"
         passwordAttemptWindow="8"             
         />
  </providers>
</membership>    
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>

I've tried: 我试过了:

 Membership.Provider.ValidateUser(username, password)

as well. 也一样 It always returns false and the Providers attributes do not match the attributes of my default provider (when I set up breakpoints and observe the object). 它总是返回false,并且Providers属性与默认提供程序的属性不匹配(当我设置断点并观察对象时)。

Anyone have any ideas how to resolve this issue? 任何人都有解决此问题的想法吗? I feel like it should be simple, seeing as the data provider is working fine on .aspx pages, but I can't for the life of me get a .cs file to use the same provider, or even see that the provider exists. 我觉得应该很简单,因为数据提供程序在.aspx页上可以正常工作,但是我一生都无法获得.cs文件来使用相同的提供程序,甚至无法看到该提供程序存在。

Please see the first answer given by LordHits on How do I call Initialize on a custom MembershipProvider? 请参阅LordHits给出的关于如何在自定义MembershipProvider上调用Initialize的第一个答案

That probably is the answer to your problem. 那可能就是您问题的答案。

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

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