post

Return additional info to the client using OAuth bearer tokens generation and Owin in WebApi

I am currently implementing token based authentication using ASP.Net Web Api 2, Owin and Identity. The client is implemented using ExtJS framework, and the login form itself (Ext.form.Panel) is requesting additional info in order to recognize if the action was successful. The additional info is consisted of one property named “success” whose value (true/false) is used to fire a proper event handler (success or failure);

In order to accomplish this task, one must adjust his OAuthAuthorizationServerProvider implementations GrantResourceOwnerCredentials method by creating additional AuthenticationProperties.

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{

context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

using (AuthRepository _repo = new AuthRepository())
{
IdentityUser user = await _repo.FindUser(context.UserName, context.Password);

if (user == null)
{
context.SetError("Invalid grant", "The user name or password is incorrect.");
return;
}
}

var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("sub", context.UserName));
identity.AddClaim(new Claim("role", "user"));

//added success parameter so that Ext.JS form can recognize action as successful
var props = new AuthenticationProperties(new Dictionary<string, string>
{
{
"success", "true"
}
});

var ticket = new AuthenticationTicket(identity, props);
context.Validated(ticket);

}

Additionally, you have to make sure that you override TokenEndPoint method to get those properties as a response when you obtain the token successfully. Without this end point the properties will not return in the response. Notice that i have removed all parameters with name that starts with dot so that “.expires” and “.issued” do not get added to the response (i do not need them).

public override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
{
//removed .issued and .expires parameter
if (!property.Key.StartsWith("."))
context.AdditionalResponseParameters.Add(property.Key, property.Value);
}

return Task.FromResult<object>(null);
}

That’s it. Hope it helps. Happy coding!

Create new Oracle user/schema

Let me first wish you a Happy New Year, this is my first post in 2016 so it would be rude from me not to mention this 🙂 Let the 2016 be prosperous for each and every one of you out there.

Now, back to business 🙂 We have migrated to the XE version of the Oracle database, as our needs do not request the full version anymore. Although the migration spared us some bucks as we are not paying the full licence anymore, it came with an additional cost – time needed to recreate everything. Since the XE version does not allow us multiple db instances, we have to create a new user/schema for each database we want to have. Additionally, i choose that we create separate tablespaces for every schema, just to keep it clean.

So first thing we need to do is login to the sqlplus with our system account (with the sysdba role), in order to add new tablespaces and create schema:

connect system/PASSWORD@XE as sysdba

Now, we can create new tablespaces with autoextend functionality. Please adjust the tablespace name and location to suit your needs:

CREATE TABLESPACE kuba2016_DATA DATAFILE 'C:\oraclexe\app\oracle\oradata\XE\kuba2016_data.dbf' SIZE 300M REUSE
DEFAULT STORAGE ( INITIAL 160K NEXT 160K MINEXTENTS 1 MAXEXTENTS 121 PCTINCREASE 1);
ALTER DATABASE DATAFILE 'C:\oraclexe\app\oracle\oradata\XE\kuba2016_data.dbf' AUTOEXTEND ON;

CREATE TABLESPACE kuba2016_INDX DATAFILE 'C:\oraclexe\app\oracle\oradata\XE\kuba2016_indx.dbf' SIZE 250M REUSE
DEFAULT STORAGE ( INITIAL 160K NEXT 160K MINEXTENTS 1 MAXEXTENTS 121 PCTINCREASE 1);
ALTER DATABASE DATAFILE 'C:\oraclexe\app\oracle\oradata\XE\kuba2016_indx.dbf' AUTOEXTEND ON;

One the tablespaces are created, we are ready to create new user/schema with needed privileges:

create user KUBA2016 identified by KUBA2016 default tablespace kuba2016_DATA;
GRANT create session to KUBA2016;
GRANT CREATE VIEW TO KUBA2016;
GRANT CREATE TABLE TO KUBA2016;
GRANT CREATE SESSION TO KUBA2016;
GRANT CREATE INDEXTYPE TO KUBA2016;
GRANT CREATE TRIGGER TO KUBA2016;
Grant CREATE DATABASE LINK to KUBA2016;
GRANT CREATE PROCEDURE TO KUBA2016;
GRANT EXECUTE ANY PROCEDURE TO KUBA2016;
GRANT CREATE ANY DIRECTORY TO KUBA2016;
COMMIT;
ALTER USER KUBA2016 quota unlimited on kuba2016_DATA;
ALTER USER KUBA2016 quota unlimited on kuba2016_INDX;

That is it, the new user/schema has been created and all we need to do now is import the oracle datapump. This can prove to be very tricky as we do not always know the name of the schema and tablespaces needed in order to import the datapump. If this is the case, I suggest that we extract DDL from the backup file using impdp:

impdp '/ as sysdba' dumpfile= logfile=import_log.txt sqlfile=ddl_dump.txt

If you examine the output file, you will realize if the schema needs to be remapped and/or additional tablespaces need to be created or remapped. I have to remap my schema and two tablespaces so my impdp command is probably more complicated than your. If you do not need to do this, just remove the last three parameters.
All you need to do now is import the datapump using impdp command:

impdp system/PASSWORD@XE directory=DATA_PUMP_DIR dumpfile=DPUMP.dmp logfile=log.log remap_schema=KUBA5_NOPIC:KUBA2016 remap_tablespace=KUBA_DATA:KUBA2016_DATA remap_tablespace=KUBA_INDX:KUBA2016_INDX

And that is it! Hope it helps