>> On this week's Essential API of the Week,
we take a look at secure storage which is
a great API to store strings securely. So, tune in.
[MUSIC]
>> Welcome back to another Xamarin Essentials API of the Week.
Today we're taking a look at secure storage.
Now, this is one of my favorite APIs because it is
the extension to the preferences API.
Well, with preferences, we have
this key value store of storing arbitrary data like booleans,
integers and strings, but these are in the open.
There are clear text if you will that are saved to disk.
When you need to store things like OAuth Tokens or usernames,
you don't want to put them there,
you want to put them into secure storage.
Now, its great about this secure storage API is that it
uses the key chain and keystore on both iOS,
Android and Windows as well.
So, this means that your
credentials or anything that you storing and secure storage
is encrypted on disk,
which is super nice.
So, let's see how we can integrate it into our app.
Here we are on the Xamarin documentation page,
where I get started every single time
with Essential API of the Week.
Then if you're on Xamarin Forms,
Android or iOS, you're gonna see Xamarin Essentials.
When you tap on that, you'll get all of the documentation you need
for Xamarin Essentials and all of the APIs here.
Now, there is a great Getting Started guide,
which will guide you through installing
the new get packages and any additional setup that you may need.
Specifically, on Android, you're going
to need to add a little bit of
initialization code and permission
support to ensure Xamarin Essentials is set up.
Now, inside of Secure Storage,
this is a more complex API.
So, there is a little bit of additional setup to
use this API that you need to be aware of.
So here, make sure of course read that getting started guide.
For Android, there's a little bit of information to understand
how Secure Storage works with auto backup.
We guide you through enabling and disabling auto backups.
If you want those secure credentials to go
between uninstalls and new devices and different devices,
you want to either enable or disable.
I prefer to do this selective backup,
which enables us to filter
out just the secure storage and nothing else.
So there's a little bit here and it's
not required, just recommended.
Now, be aware also on iOS,
there's a little bit of key chain authentication that you need to
set up when you're in a simulator mode on UWP,
nothing else to do.
Now, I do love this API because it's super simple, check this out.
When you want it set an item in the Secure Storage,
you set SetAsync, give it
the key and give it the value that you want to set.
Now, it only takes in a string because
that's how it is encrypted on disk,
but you'll have it right there.
Another nice thing here is that you can also say, GetAsync.
Now, this will return a value or possibly
could throw an exception if that's not supported on the disk.
So, you want to use that there. That's it.
You can also remove,
you can say RemoveAll and you can
even learn about the different implementation details.
Because again, this is a little bit more of a complex API.
But let's integrate this into our app.
Our app here's this little login app
and previously we've used preferences
to save usernames and
preferences but nothing for this password yet.
So, it would be a little bit weird
to put in preferences and not recommended.
We want to put that in secure storage.
So, here on Android, what
we're going to first do is we're going to
go into our NuGet Package Manager
and we'll install Xamarin Essentials
into all of our apps and libraries.
So, here I've already installed it into Android,
iOS and my.NET Standard 2.0 Library.
So, I have access to those underlying APIs and the abstraction.
Now, here inside of main activity,
I have my initialization code and
that requests permission that we saw from the documentation.
So, what we're going to do here is when we go to login,
whenever we login successfully,
that is when we'll want to save that password into Secure Storage.
So, here's my Handle_Clicked event where I'm checking
Internet access and making
sure that the password is strong enough.
So, here what we'll go ahead and do just for this demo purpose,
will go ahead and clear out the information when it is valid,
and what we're going to do as long as this valid,
is we're just going to set this into Secure Storage.
So, let's go over and actually just copy
and paste the code from the documentation.
Will hit "Copy" Come over here and hit "Paste".
Now, we'll just call this token,
because that seems pretty okay,
and then what we'll also do is we're
going to say PasswordEntry.Text.
So, whatever that password is,
we'll save it right into Storage.
We had a breakpoint here just in case
something goes wrong. But that's all right.
Then we're going to go ahead and get it.
So, we'll go ahead and copy the Getcode.
Now, this is asynchronous.
So, there's Async away.
So we're going to want to put this in
a place where we can handle that.
So, we can't really put it in our constructor,
we will put it in our OnAppearing.
So, we'll call this override async void
and we'll go ahead and get that back.
Again, this will be the token.
So, here we'll go ahead and now
say PasswordEntry.Text equals oauthToken,
on in this case we'll just call it password.
There we go. So, now what we'll do is we can come
in and add a break point here and hit "Debug".
Now, the first time we launch it,
we shouldn't have anything into
our token since we haven't said it yet,
but the next time we launch it,
we should get that password back.
So, we'll give it a second to redeploy.
There we go. We've hit in our breakpoint.
So, we'll go ahead and continue and what we'll see here is that
the actual text value is null.
There's nothing set on it at all
because that's what our password was set too.
So, now what we'll do is we'll
go ahead and let the page load and we'll
enter our password of test1234 and hit "LOGIN".
Now, this will go ahead and save it to
the Secure Storage part of Xamarin Essentials.
Now, all we need to do is quit the application.
We'll go ahead and launch it one more time.
So, now we're in a new debug session,
but we've saved it into Secure Storage.
So, ideally, we'll see is that now
our password field will be 1234.
There we go. We've hit our breakpoint.
Our text is currently null and as I step over,
we can see now that it's set to 1234.
The difference is that with
secure storage that's been encrypted to disk,
it's not going to be crackable at all and it's stored in
the key-chain and keystore on both iOS and Android.
There you have it with just a few lines of code.
Secure storage is updated and into our application.
That is just one of many APIs of Xamarin Essentials.
So, be sure to read the documentation in the links below to learn
more about the amazing library that's
available to you as a Xamarin developer.
That's going to do it for this Essential API
of the Week. Thanks for watching.
[MUSIC]