Extend available properties of User.Identity – Asp.Net [web api]

Asp.net web api by default contains some pre configured fields that can handle the registration of a user in a web app. Some of them are Email, Password, Confirm Password and others. You can extend those properties and include your own for your purposes with the following procedure.

First of all you need to execute some commands in the package manager. You can find nuget package manager in Tools -> Nuget Package Manager -> PM Console 

The first thing to do is to enable Migrations

The you can go to Models\AccountBindingModels.cs and add your property to the class RegisterBindingModel. Also add the same property in the Models\IdentityModels.cs inside ApplicationUser class. For example lets assume you want to add a username in the registration proccess. For this purpose you can use the following line

Also include in the file.

The you must execute:

Those commands will run all the migration files and update the database schema to add a AppUserName column in your database.

Then update the registration action in Controllers\AccountController.cs to store AppUserName as well.

It was that easy. you can finally find AppUserName in your database.

Display Herocard in BotFramework

As the botframework evovles, many things are changing in the apis. I wanted to create a Herocard for a bot and the previously existed method wasn’t working, so i find the new way that one can reply herocards in the user. The main function needed is the

 

Full C# example

 

The result will look like that

 

This github repo is very useful as it includes many botframework samples.

Application Insights VS2015 failed to add applicationinsights.config

If you ever deal with the problem

in Visual studio 2015 and Windows 10 you only have to create a file and name it  ApplicationInsights.config with the code below. That’s because vs and nuget cannot create this file in the root folder of your project.

You must enter your specific InstrumentationKey for the application that you have registered through Azure portal for Insights.

The trick is that you must select Build Action as Content and Copy always to output folder

vs2015insights

UnauthorizedAccessException C# File.IO Async

I had to upgrade one application and put some extra functionality. So i added an async function that handles a .txt file and edit some fields.

So i used this code

 

But i also had a function in MainPage that asynchronously was reading the same file

 

Both functions were async so we could not determine when each function was running and which will be executed first.

So when i tried to run the app i came across with UnauthorizedAccessException because both function maybe were trying to access the file the same time. So i have to make one function wait. You can achieve that by many ways. I used the below function.

 

 

But i want to execute this code only once. So i used localSettings