Custom Authentication in Oracle APEX

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

8/18/2019 Custom Authentication in Oracle APEX

MENU

Custom Authentication in Oracle APEX

View more Tutorials:

Oracle APEX Tutorials

1- Introduction
2- Default authentication of APEX
3- SQL Script
4- Declaring Application Items
5- Custom Authentication

108
Shares

1- Introduction

https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 1/19
8/18/2019 Custom Authentication in Oracle APEX

This document is based on:

Oracle APEX 5.0

2- Default authentication of APEX


When you create an Oracle APEX application, the default login page is created with page number is 101. Default
login uses APEX authentication, which means you have to enter a username and password created by the APEX
Admin. In case you have a separate table to store user information, you need to customized authentication.

OK this is the default login page is created:

3- SQL Script x

https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 2/19
8/18/2019 Custom Authentication in Oracle APEX

Try 30 days free. SIGN UP

To begin this example, you need to run Script to create table to store user and create package.

Create USER_ACCOUNT table:

?
1 create table USER_ACCOUNT
2 (
3   USER_NAME VARCHAR2(30) not null,
4   PASSWORD  VARCHAR2(30) not null,
5   USER_TYPE VARCHAR2(10) not null,
6   ACTIVE    VARCHAR2(1) not null,
7   EMAIL     VARCHAR2(64) not null,
8   FULL_NAME VARCHAR2(64) not null
9 );
10   
11 alter table USER_ACCOUNT
12   add constraint USER_ACCOUNT_PK primary key (USER_NAME) ;
13 alter table USER_ACCOUNT
14   add constraint USER_ACCOUNT_UK unique (EMAIL) ;
15  
16 -----------------------------------
17  
18 insert into user_account (USER_NAME, PASSWORD, USER_TYPE,
19  ACTIVE, EMAIL, FULL_NAME)
20 values ('tom', 'tom123', 'admin', 'Y', '[email protected]', 'Tom');
21  
22 insert into user_account (USER_NAME, PASSWORD, USER_TYPE,
23 ACTIVE, EMAIL, FULL_NAME)
24 values ('jerry', 'jerry123', 'user', 'Y', '[email protected]', 'Jerry');
25  
26 insert into user_account (USER_NAME, PASSWORD, USER_TYPE,
27 ACTIVE, EMAIL, FULL_NAME)
28 values ('donald', 'donald123', 'guest', 'N', '[email protected]', 'Donald');
29  
30 Commit;

PKG_SECURITY
x?
1 Create Or Replace Package Pkg_Security Is
2  
3   Function Authenticate_User(p_User_Name Varchar2
4                             ,p_Password  Varchar2) Return Boolean;
5  

https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 3/19
8/18/2019 Custom Authentication in Oracle APEX
6
7
  -----
  Procedure Process_Login(p_User_Name Varchar2 x
8                          ,p_Password  Varchar2
9                          ,p_App_Id    Number);
10  
11 End Pkg_Security;
12 /
13 Create Or Replace Package Body Pkg_Security Is
14  
15   Function Authenticate_User(p_User_Name Varchar2
16                             ,p_Password  Varchar2) Return Boolean As
17      v_Password User_Account.Password%Type;
18      v_Active   User_Account.Active%Type;
19      v_Email    User_Account.Email%Type;
20   Begin
21      If p_User_Name Is Null Or p_Password Is Null Then
22   
23         -- Write to Session, Notification must enter a username and password
24         Apex_Util.Set_Session_State('LOGIN_MESSAGE'
25                                    ,'Please enter Username and password.');
26         Return False;
27      End If;
28      ----
29      Begin
30         Select u.Active
31               ,u.Password
32               ,u.Email
33         Into   v_Active
34               ,v_Password
35               ,v_Email
36         From   User_Account u
37         Where  u.User_Name = p_User_Name;
38      Exception
39         When No_Data_Found Then
40       
41            -- Write to Session, User not found.
42            Apex_Util.Set_Session_State('LOGIN_MESSAGE'
43                                       ,'User not found');
44            Return False;
45      End;
46      If v_Password <> p_Password Then
47     
48         -- Write to Session, Password incorrect.
49         Apex_Util.Set_Session_State('LOGIN_MESSAGE'
50                                    ,'Password incorrect');
51         Return False;
52      End If;
53      If v_Active <> 'Y' Then
54         Apex_Util.Set_Session_State('LOGIN_MESSAGE'
55                                    ,'User locked, please contact admin');
56         Return False;
57      End If;
58      ---
59      -- Write user information to Session.
60      --
61      Apex_Util.Set_Session_State('SESSION_USER_NAME'
62                                 ,p_User_Name);
63      Apex_Util.Set_Session_State('SESSION_EMAIL'
64                                 ,v_Email);
65      ---
66      ---
67      Return True;
68   End;
69  
70   --------------------------------------
71   Procedure Process_Login(p_User_Name Varchar2
72                          ,p_Password  Varchar2
73                          ,p_App_Id    Number) As
74      v_Result Boolean := False;
75   Begin
76      v_Result := Authenticate_User(p_User_Name
77                                   ,p_Password);
78      If v_Result = True Then
79         -- Redirect to Page 1 (Home Page).
80         Wwv_Flow_Custom_Auth_Std.Post_Login(p_User_Name -- p_User_Name
81                                            ,p_Password -- p_Password
82                                            ,v('APP_SESSION') -- p_Session_Id
83                                            ,p_App_Id || ':1' -- p_Flow_page
84                                             );
85      Else
86         -- Login Failure, redirect to page 101 (Login Page). x
87         Owa_Util.Redirect_Url('f?p=&APP_ID.:101:&SESSION.');
88      End If;
89   End;
90  
91 End Pkg_Security;
92 /
https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 4/19
8/18/2019 Custom Authentication in Oracle APEX

4- Declaring Application Items


Click on the "Shared Components", here allows you to declare the "Application Items", it is the items will be used in
your application.

https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 5/19
8/18/2019 Custom Authentication in Oracle APEX

https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 6/19
8/18/2019 Custom Authentication in Oracle APEX

Enter a name for the Application Item is "LOG_MESSAGE", its value is "LOGIN_MESSAGE" attribute of Session, you
can set its value in PL/SQL:

?
1 Apex_Util.Set_Session_State('LOGIN_MESSAGE'
2                                              ,'User not found');

https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 7/19
8/18/2019 Custom Authentication in Oracle APEX

https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 8/19
8/18/2019 Custom Authentication in Oracle APEX

Similarly, you create 2 Application Items:

SESSION_USER_NAME
SESSION_EMAIL

https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 9/19
8/18/2019 Custom Authentication in Oracle APEX

5- Custom Authentication x

Open LOGIN page in APEX Page Designer:

Create new Region:

https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 10/19
8/18/2019 Custom Authentication in Oracle APEX

https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 11/19
8/18/2019 Custom Authentication in Oracle APEX

Change the properties for the newly created Region.

https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 12/19
8/18/2019 Custom Authentication in Oracle APEX

Set display conditions for this Region.

https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 13/19
8/18/2019 Custom Authentication in Oracle APEX

Next you have to modify code handling Process when the user clicks the Submit button. x

https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 14/19
8/18/2019 Custom Authentication in Oracle APEX

PL/SQL Code:

?
1 Pkg_Security.Process_Login(:P101_USERNAME,
2                            :P101_PASSWORD,
3                            :APP_ID);

https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 15/19
8/18/2019 Custom Authentication in Oracle APEX

Save and running your apps:

https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 16/19
8/18/2019 Custom Authentication in Oracle APEX

View more Tutorials:

Oracle APEX Tutorials

Try 30 days free. SIGN UP

report this ad

Oracle APEX Tutorials

What is Oracle Application Express? x


Installing and configuring Oracle Apex 5.0
Install Oracle REST Data Services (ORDS) for Oracle APEX
Oracle APEX Tutorial for Beginners (APEX 5.0)
https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 17/19
8/18/2019 Custom Authentication in Oracle APEX

Oracle APEX Tabular Form x


Oracle APEX Master Details
Custom Authentication in Oracle APEX
Oracle APEX Dynamic Action
Create Dynamic Content Region in Oracle APEX (PL/SQL Dynamic Content)
What is Business Intelligence?
Installing and configuring Oracle BI 11g

SPONSORED SEARCHES

admission open

apex authentication

apex services

app creating

app design

report this ad

Newest Documents

HTML Hyperlinks
HTML Lists
HTML Images
HTML Tables
Installing Atom-Beautify
Starting with HTML
HTML Editors
Installing Atom HTML Preview
Introducing HTML
Introducing Javascript HTML5 Canvas API

https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 18/19
8/18/2019 Custom Authentication in Oracle APEX

Try 30 days free. SIGN UP

report this ad

o7planning.org

https://o7planning.org/en/10443/custom-authentication-in-oracle-apex 19/19

You might also like