Xamarin Android SDK
The following things are to be kept in mind while integrate our cidaas sdk
Configuring App/Client:
When you are integrating your own Business App with cidaas, you may want to modularize the interactions and attributes. There are several information like Scope, Roles, Grant-Types, re-direct URLs etc., that you may want to group into one configuration/settings. This can be done by creating [cidaas App or Client]/(manage-applications/app-settings/native-apps/android-app.md).
The steps here will guide you through set up and management of authentication and authorization in your apps using cidaas Xamarin Android SDK.
Click here for Sample Project.
Requirements
Visual Studio : 2017 Minimum Android Version : API level 21
Installation
CidaasSDKAndroid is available through NuGet. There are 3 options to integrate it in your project
1 . Enter the following command in Package Manager Console
`Install-Package cidaas-sdk-android-v2`
2 . Download the package from the following url
https://www.nuget.org/packages/cidaas-sdk-android-v2
Once downloaded, drag and drop the file to your project
3 . Right click on your project and select Add Nuget Packages option. Search for cidaas-sdk-android-v2 and install it
Getting Started
Create an XML file and add the following code
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<item name="DomainURL" type="string">Your Domain URL</item>
<item name="ClientId" type="string">Your Client Id</item>
<item name="RedirectURI" type="string">Your redirect URL</item>
</resources>
Set the file name of your XML file using your AssetManager
CidaasSDKView.SetFileURL(asset, "Cidaas.xml");
Login
To perform login operation, copy and paste the following code snippet
var cidaas = new CidaasSDKView(this);
cidaas.Login(rel, (response) =>
{
// your response code here
// user_id = response.UserInfoEntity.sub;
// access_token = response.TokenEntity.access_token;
});
Getting User Information
To get user information, call GetUserInfoByAccessToken() method
CidaasSDKView.GetUserInfoByAccessToken(access_token);
Getting Access token
To validate access token, call GetAccessToken() method
CidaasSDKView.GetAccessToken(user_id);
Logout user
To logout the session, call Logout() method
CidaasSDKView.Logout(user_id);
Sample Code
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:minHeight="?android:attr/actionBarSize"
android:background="@color/colorOrange"
android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="@+id/cidaas"
android:layout_below="@id/toolbar"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_width="match_parent" />
</RelativeLayout>
MainActivity.cs
using Android.App;
using Android.Widget;
using Android.OS;
using CidaasSDKAndroid.Views;
using CidaasSDK.Helpers;
using Android.Views;
using Android.Content;
using Android.Content.Res;
using System;
using System.Xml;
using System.IO;
using System.Net.Http;
using Newtonsoft.Json;
using System.Threading.Tasks;
namespace AndroidExampleXamarin
{
[Activity(Label = "AndroidExample", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
private CidaasSDKView cidaas;
string user_id, access_token;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
var toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
SetActionBar(toolbar);
ActionBar.Title = "Cidaas SDK";
this.Window.AddFlags(WindowManagerFlags.Fullscreen);
RelativeLayout rel = FindViewById<RelativeLayout>(Resource.Id.cidaas);
cidaas = new CidaasSDKView(this);
AssetManager asset = this.Assets;
CidaasSDKView.SetFileURL(asset, "Cidaas.xml");
var loginUrl = CidaasSDKView.GetLoginURL();
var registerUrl = CidaasSDKView.GetRegisterURL();
Console.WriteLine("Login URL - " + loginUrl);
Console.WriteLine("Register URL - " + registerUrl);
cidaas.Login(rel, (obj) =>
{
user_id = obj.UserInfoEntity.sub;
access_token = obj.TokenEntity.access_token;
bool check = CidaasSDKView.validateToken(access_token);
if (check)
{
var profile = new Intent(this, typeof(ProfileActivity));
profile.PutExtra("userId", user_id);
profile.PutExtra("accessToken", access_token);
StartActivity(profile);
}
});
}
public override void OnBackPressed()
{
if (cidaas.BackPressed())
{
return;
}
base.OnBackPressed();
}
}
}