How do I setup ASP.NET Identity 2.0 to use my own connection string

By default ASP.NET Identity 2.0 creates the database inside the App_Data directory of the application. ASP.NET identity 2.0 works on the Entity Framework Code First approach and it reads the connection string mentioned in the web.config as shown below,

clip_image002

In the ApplicationDbContext class you can find that connection string named DefaultConnection has been used. You can find ApplicationDbContext class inside the IdentityModels.cs file.

clip_image003

You may have a requirement to use some other database server and the database to configure the ASP.NET Identity 2.0. Simplest way you can do this by changing the connection string the web.config file.

clip_image005

DJPC is database server name of my development machine. ASP.NET Identity 2.0 works on the EF Code First approach, it will create the database DelhiDevDayExample, if it’s not already there. Other option is to create connection string programmatically and pass it to the context class.

This is the way you can change the connection string of ASP.NET Identity 2.0.

Hindi Video : Create Azure based ASP.NET MVC application in 30 minutes

 

Subscribe here to YouTube channel for all episodes

image

Cloud has been a buzz word for a while now. As a developer, we focus on converting the customer requirements to the code and sometimes find ourselves behind with the industry buzz words. The cloud is everywhere and perhaps the future. However as a developer, you won’t have to write very different code for cloud than codes you have been writing for the in-premise applications. In this post, I assume that you are already aware about MVC applications, hence I will focus on demonstrating how building for cloud is not very much different. I will follow step by step approach and create end to end MVC application completely hosted in the cloud (data and application both).

In this article, we will follow the following steps,

  • Setting up the database in the SQL Azure
  • Creating the MVC application
  • Building the application using MVC and the Entity framework
  • Publishing the application to Azure web site

Read full article on Infragistics blog

Subscribe here to YouTube channel for all episodes

 

 

 

 

 

Read article based on this video here

Subscribe here to YouTube channel for all episodes

I hope you like this video. Do not hesitate to leave comment or any feedback. Happy coding.

How to create a Container in Azure Storage from MVC application

I am working on a MVC application in which I need to work with Microsoft Azure storage .I had a requirement to create An Azure storage container from the MVC application. In this post I will share my learning that how I created a container programmatically.

I created the view as shown in below image. User has to enter the azure storage account name, azure storage key and the name of the container to be created.

image

Above View is MVC razor view, and it is created using the cshtml as shown below:

 


@{
    ViewBag.Title = "BLOB Manager";
}
<div class="jumbotron">
    <div class="row">
        <div class="col-md-8">
            <section id="loginForm">
                @using (Html.BeginForm("AzureInfo", "Manage", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
                {
                    @Html.AntiForgeryToken()
                    <h2> Manage BLOB Container</h2>
                    <hr />
                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                    <div class="form-group">
                        <div class="col-md-10 input-group input-group-lg">
                            @Html.TextBox("AccountName", "", new { @class = "form-control", @placeholder = "Account Name" })
                            @Html.ValidationMessage("AccountName", "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="form-group">

                        <div class="col-md-10 input-group input-group-lg">
                            @Html.TextBox("AccountKey", "", new { @class = "form-control", @placeholder = "Account Key" })
                            @Html.ValidationMessage("AccountKey", "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="form-group">

                        <div class="col-md-10 input-group input-group-lg">
                            @Html.TextBox("ContainerName", "", new { @class = "form-control", @placeholder = "Container Name" })
                            @Html.ValidationMessage("containername", "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="form-group input-group input-group-lg">
                        <div class="col-md-offset-2 col-md-10">
                            <input type="submit" value="Create Container" class="btn btn-lg btn-success" />
                        </div>
                    </div>

                }
            </section>
        </div>
    </div>
</div>

Above I am creating,

  • Three text boxes with the placeholder and the class set to bootstrap form-control
  • A submit button with the class set to bootstrap btn-lg and btn-success
  • All the controls are inside the form.
  • On form post operation, AzureInfo action of the Manage controller will be called.

Once the View is ready, let us go ahead and create an azure utility class. In this class we will put all the operations related to the azure storage. But before that you need to add Azure storage library to the project. Either you can use NuGet package manager or console to add the library. I am using console to install Azure storage library. I have taken the 4.3 version because at the time of writing this post, this was the latest stable version available.

image

Once package is successfully installed, add following namespaces.

 


using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure;

To create the container in the Azure storage, I have created a function CreateContainer. This function takes three input parameters, account name, account key and the container name.

 


public static class AzureUtility
    {
        public static string  CreateContainer(string AccountName, string AccountKey, string ContainerName)
        {

            string UserConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", AccountName, AccountKey);
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(UserConnectionString);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(ContainerName.ToLower());
            if (container.CreateIfNotExists())
            {
                container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
                return container.Name;
            }
            else
            {
                return string.Empty;
            }
        }
    }

Now let us examine the above code line by line.

  • In first line I created the connection string for the azure storage. It takes two parameters, account name and the account key. These values are passed as input parameter to the function. I am using these two parameter to create connection string.
  • Using CloudStorageAccount to parse the connection string from the constructed string.
  • Creating the CloudBlobClient
  • Getting the container reference of the BLOB.
  • If container reference does not exist, creating new container.
  • On successful creation of the container function returns name of the container else empty string.

 


[HttpPost]
        public ActionResult AzureInfo(string AccountName, string AccountKey, string ContainerName)
        {

           var result= AzureUtility.CreateContainer(AccountName, AccountKey, ContainerName);

           if (result != string.Empty)
             {

                 return RedirectToAction("Index", "Home");
             }
             else
             {
                 return RedirectToAction("Index");
             }
        }

In the action we are using the utility class to create the container. In this way an azure container can be created from the MVC application.

I hope it is useful. Thanks for reading. Happy Coding.

Child Actions in ASP.NET MVC

Read full post on falafel blog

Child Actions are the action methods which can be invoked within the view. This is used to work with the data in the view, which are not related to the main action method. For example, if you want to create a data driven widget in the view using data that is not related to the main action method, you will need to use the child action method.
In ASP.NET MVC any action can be used as a child action. However, to use an action only as a child action and attribute it with the ChildActionOnly. It will make sure the action is not called by any user request and will only be used in the view. A child action can be created as shown below:

Read full post on falafel blog

Check User exists in a Role in ASP.NET Identity 2.0

Read full post on Falafel blog

ASP.NET MVC5 uses Identity 2.0 to manage the users and their roles. There may be scenario to check whether the currently logged in user or any user with the id exists in the given role or not, and checking can be done either in the controller class or in any other class of the application.

Check for the currently logged in user
In a controller you can find whether the currently logged in user exist in a role or not by using User property of the controller class. Type of the User property is IPrincipal and it has a Boolean method IsInRole which takes the role name as input and returns true if the currently logged in user exist in the role.

Read full post on Falafel blog