简体   繁体   English

在ASP.NET中验证Oracle用户

[英]Authenticate Oracle users in ASP.NET

I have database in Oracle and application designed in ASP.NET. 我在Oracle中有数据库,在ASP.NET中设计了应用程序。 I want to check user authentication before they can access application. 我想在访问应用程序之前检查用户身份验证。 Users are created in Oracle not in application's table. 用户在Oracle中创建,而不是在应用程序表中创建。

Is it possible to authenticate Oracle database users using asp.net? 是否可以使用asp.net对Oracle数据库用户进行身份验证?

The application uses WebForms and Oracle .NET Data Provider library. 该应用程序使用WebForms和Oracle .NET数据提供程序库。

EDIT 编辑

  1. First page is login where user enters Oracle username and password 第一页是登录,用户输入Oracle用户名和密码
  2. System connects to Oracle and authenticates user. 系统连接到Oracle并对用户进行身份验证。 Please remember users are created in Oracle not in application tables. 请记住用户是在Oracle中创建的而不是在应用程序表中创建
  3. If valid user then show application pages. 如果有效用户则显示应用程序页面。

(I can successfully connect to Oracle but cannot authenticate Oracle users) (我可以成功连接到Oracle,但无法对Oracle用户进行身份验证)

This sample web form app follows the steps outlined HERE in order to authenticate users from an Oracle database. 此示例Web表单应用程序遵循此处概述的步骤,以便从Oracle数据库对用户进行身份验证。 The app creates a RestrictedPage in a restricted folder accessible only to authenticated users. 该应用程序在受限文件夹中创建RestrictedPage ,仅对经过身份验证的用户可访问。 Anonymous users trying to access the restricted page are redirected to the Login page. 尝试访问受限页面的匿名用户将重定向到“ Login页面。 And once an authenticated user logs out, the app redirects it to the Default home page. 一旦经过身份验证的用户注销,该应用程序就会将其重定向到Default主页。

在此输入图像描述

RestrictedPage.aspx: RestrictedPage.aspx:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <h1>Restricted Page</h1>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="EMPLOYEE_ID" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:BoundField DataField="EMPLOYEE_ID" HeaderText="EMPLOYEE_ID" ReadOnly="True" SortExpression="EMPLOYEE_ID" />
        <asp:BoundField DataField="FIRST_NAME" HeaderText="FIRST_NAME" SortExpression="FIRST_NAME" />
        <asp:BoundField DataField="LAST_NAME" HeaderText="LAST_NAME" SortExpression="LAST_NAME" />
        <asp:BoundField DataField="EMAIL" HeaderText="EMAIL" SortExpression="EMAIL" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="select employee_id, first_name, last_name, email from hr.employees where employee_id &lt; 150"></asp:SqlDataSource>
</asp:Content>

Site.Master: showing only the div of interest. Site.Master:仅显示感兴趣的div The rest is the standard boilerplate markup created by VS project template. 其余的是VS项目模板创建的标准样板标记。

<div class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        <li><a runat="server" href="~/">Home</a></li>
        <li><a runat="server" href="~/About">About</a></li>
        <li><a runat="server" href="~/Contact">Contact</a></li>
        <li><a runat="server" href="~/Restricted/RestrictedPage">Restricted</a></li>
    </ul>
    <asp:LoginView runat="server" ViewStateMode="Disabled">
        <AnonymousTemplate>
            <ul class="nav navbar-nav navbar-right">
                <li><a runat="server" href="~/Account/Register">Register</a></li>
                <li><a runat="server" href="~/Account/Login">Log in</a></li>
            </ul>
        </AnonymousTemplate>
        <LoggedInTemplate>
            <ul class="nav navbar-nav navbar-right">
                <li><a runat="server" href="~/Account/Manage" title="Manage your account">Hello, <%: Context.User.Identity.GetUserName()  %> !</a></li>
                <li>
                    <asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" OnLoggingOut="Unnamed_LoggingOut" />
                </li>
            </ul>
        </LoggedInTemplate>
    </asp:LoginView>
</div>

IdentityModels.cs: this is the method added for creating/associating entity models to the corresponding Oracle schema tables. IdentityModels.cs:这是为将实体模型创建/关联到相应的Oracle模式表而添加的方法。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder); // MUST go first.

    modelBuilder.HasDefaultSchema("YOUR_SCHEMA"); // Use uppercase!

    modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers");
    modelBuilder.Entity<IdentityRole>().ToTable("AspNetRoles");
    modelBuilder.Entity<IdentityUserRole>().ToTable("AspNetUserRoles");
    modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");
    modelBuilder.Entity<IdentityUserLogin>().ToTable("AspNetUserLogins");
}

Web.config: this is the web config file for the Restricted folder. Web.config:这是Restricted文件夹的Web配置文件。

<?xml version="1.0"?>
<configuration>
  <location path="RestrictedPage.aspx">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>
</configuration>

Web.config: these are the additions of interest made to the global application config file in the application root folder. Web.config:这些是对应用程序根文件夹中的全局应用程序配置文件感兴趣的补充。

  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=your_server_name;User ID=your_user_id;Password=xxxxxxxx;"
      providerName="Oracle.ManagedDataAccess.Client" />
  </connectionStrings>

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="Oracle.ManagedDataAccess.Client"
        type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework" />
    </providers>
  </entityFramework>

Here's the Oracle database schema showing the required AspNet.Identity tables created by the script listed in the referenced article. 这是Oracle数据库模式,显示了由引用文章中列出的脚本创建的所需AspNet.Identity表。

在此输入图像描述

This app uses Oracle's sample HR schema that can be downloaded from HERE . 此应用程序使用Oracle的示例HR模式,可以从HERE下载。

在此输入图像描述

You'll want to use the System.Data.OracleClient namespace for all you're data functions. 您将要为所有数据函数使用System.Data.OracleClient命名空间。 It behaves more or less like System.Data.SqlClient. 它的行为或多或少类似于System.Data.SqlClient。 Take a look at one of these KB articles depending on whether you are using C# or VB: VB: http://support.microsoft.com/default.aspx?scid=kb;en-us;308157 C#: http://support.microsoft.com/default.aspx?scid=kb;en-us;301240 You'll have to replace the Sql stuff with Oracle calls, but that shouldn't be too hard. 根据您使用的是C#还是VB,查看其中一篇知识库文章:VB: http: //support.microsoft.com/default.aspx? scid = kb; en-us; 308187 C#: http:// support.microsoft.com/default.aspx?scid=kb;en-us;301240您必须用Oracle调用替换Sql内容,但这不应该太难。

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

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