.NET MAUI & Storage, Part 1 - Secure Storage

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in a series of at least three episodes i will talk about dotnet maui and storage in this the first episode i will talk about secure storage [Music] welcome back to another.net maui video today and in the upcoming episodes i will talk about storage in this the first of the storage episodes i will talk about secure storage and how you can save sensitive data like access tokens connection string password and other stuff that you want to be stored in a secure way and that is pretty easy with dotnet maui and the maui essentials so i will show you how you can use it ok here i have started to build an app it's a simple app where you enter an access token and save it and then you should be able to click the button to show it so what i've done is that i have built the ui created view models but i have don't added the logic to save it yet and that is what we would do in this video so we can close the app and we can go to visual studio and our view model so here in the view model i have added the properties needed for the ui access token save access token and for the command save and show and here we have this save token method that will run when you hit save button and the show token method that will run when you hit the show button so it is in those methods where we would add some logic to save the access token and get the access token to secure storage if you run on ios for example it will save it in the keychain so what we can do here is we can do the simplest way just use the static method on the secure storage class like this secure storage dot default dot set async then we need to pass a key and the value and for the key i have created a constant access token key and just give it a value so we enter that one here access token key then we pause the access token property just like this and now we have saved the token now we can go here and get the token instead so it will also be that simple so we can say access saved access token is equal to a weight secure storage default dot get async and then we can pass the access token key just like this and we are home okay so let's try to run the app and see if this works so here we have the app again so we enter my secret token and we save it oops here we have an exception and the problem here is that to be able to save the keychain on the ios simulator we need to add some entitlements so we will do that if you run it on a physical device you don't need to do this but for the simulator you need to do this both on ios and if you run it on mac so we go to the platforms folder ios and then we create a new file an xml file and call it an title meant dot b list like this and then i prepared how that one should look so we open it and we will just do the code we paste the value from another app that i have and here we can see that we have a dictionary of keys keychain access group and here we have an array of strings so here we add an identifier for this keychain access group and then we save it then we can go here to vicious dude again we go to prop j properties on the project and then we can say custom entitlements is this info plist file yes need to be in the right folder of course so then we also need to set a cockpit output directory to have it work so now we can try to run the app again so here we have the app again and let's try again with my secret token and save okay and now we didn't have an exception so let's see if we can show it it worked the only problem we have now is that this code is not that testable because we cannot run a unit test and have this secure storage code inside of the view model so what i recommend you to do is to create an own service that handles the secure storage part and have an interface then you use dependency injection to inject that service into the view model so let me show you how you can do that so we create a new class or an interface maybe i uh secure storage service don't call a secure storage because there are already an interface in maui that is called that but you cannot use that one in an easy way because there are no single implementation of it all platform has its own implementation so i prefer creating an own service and that also makes it more flexible to use other options if you for example want to reuse some of the code in let's say a blazer app if you're doing maui blazer you maybe want to have the service also on the web and then you can have an other implementation but not using secure storage may be stored in a local storage or cookies or whatever so we have in here in the face task save string key string value and task that returns a string get and we pause a key to it okay that's all then we create a new class secure storage service and we implement the interface like this and then we take this securestorage.default.getasync pass the key and then we do like this secure storage dot set async key value and add the await keyword to both of those and then we need also the method of async and we're done and now we can go to the maui program class and we can map this interface to the implementation so builder so builder services dot add maybe singleton i see your storage service is mapped to say cure storage service like this okay and now we can go to the view model we can inject it i secure storage service create a read-only field for it and then we can use it here so let's see your storage service save pass the same data and here we do the same secure storage source but we use the get method of course so now this is much more testable and you can write unit tests for your view model so let's end this video with testing if this works so here we add a token just like that save and show it works thank you for watching this video about maui storage and secure storage in the next video i will talk about how you can store settings in your.net maui app so don't forget to subscribe to my channel and like this video and we see you next time
Info
Channel: Daniel Hindrikes
Views: 8,062
Rating: undefined out of 5
Keywords:
Id: Yi7SAyh7k8w
Channel Id: undefined
Length: 10min 16sec (616 seconds)
Published: Tue Aug 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.