Facebook external login using oauth returns null email


Need to install Facebook SDK from NuGet packages.
In StartUp File
app.UseFacebookAuthentication(new FacebookAuthenticationOptions { AppId = "XXXXXXXXXX", AppSecret = "XXXXXXXXXX", Scope = { "email" }, Provider = new FacebookAuthenticationProvider { OnAuthenticated = context => { context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken)); return Task.FromResult(true); } } });
In Controller or Helper
var identity = AuthenticationManager.GetExternalIdentity(DefaultAuthenticationTypes.ExternalCookie); var accessToken = identity.FindFirstValue("FacebookAccessToken"); var fb = new FacebookClient(accessToken); dynamic myInfo = fb.Get("/me?fields=email,first_name,last_name,gender"); // specify the email field
Sorce: Facebook external login using oauth returns null email

Twitter external login using oauth returns null email


For getting twitter user email using oauth external login we need to getting the Twitter support to allow the permission on our app. Please refer this link : Get user’s email address from twitter OAuth API in .Net applications

ViewData, ViewBag, TempData in MVC

In MVC there are three ways - ViewData, ViewBag and TempData to pass data from controller to view.

ViewData
  •  ViewData is a dictionary object that is derived from ViewDataDictionary class. public 
  •  ViewData is used to pass data from controller to corresponding view.
  •  Its life lies only during the current request.
  •  If redirection occurs then its value becomes null.
  •  It’s required typecasting for getting data and check for null values to avoid error.

ViewBag

  •  ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
  •  Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.
  • Its life also lies only during the current request.
  • If redirection occurs then its value becomes null.
TempData
  • TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.
  • TempData is used to pass data from current request to subsequent request (means redirecting from one page to another).
  • Its life is very short and lies only till the target view is fully loaded.
  • It’s required typecasting for getting data and check for null values to avoid error.
  • It’s used to store only one time messages like error messages, validation messages.

How to use wcf service in MVC 5.

In this article, I m explain How to use wcf service in MVC 5.

In previous post I explained How to Create WCF Service in Visual Studio 2013.

Now Continue with previous post.

Create new project, Right Click on Solution Explorer and add new project.

Select ASP>NET Web Application with .NET Framework 4.5.


ASP.NET WEB APPLICATION

Select Empty MVC Template.


ASP.NET MVC WEB APPLICATION

Your MVC project add in solution explorer.

Now first copy wcf service url.(Please first check previous post here)


ADD SERVICE REFERENCE


Now EmployeeService service references add in your WebApplication.

SERVICE REFERENCE IN ECF

Now right click on Employee service and Click on Configure Service Reference. 

CONFIGURE SERVICE REFERENCE

Now uncheck Reuse type in referenced assemblies and click ok.

CONFIGURE SERVICE REFERENCE


Add New Controller in MVC application.
CONTROLLER IN MVC

Now add following code in Index action.


ADD VIEW IN MVC
Right click on Action method and Add View.

Now Select Template List and Model class.


ADD VIEW IN MVC

Your View code look like this.


VIEW IN MVC


Now Run Application(Ctrl + F5).


WCF APPLICATION

Enjoy..!

Areas in MVC 5

In this article, I am explain how to use area in MVC.

When we create new MVC project then Model, View and  Controller folder create automatically.

This structure is comman for simple application. but when your application gets big and complex then single model, view and controller can get complicated.So we can maintained complex mvc application using area.Using areas, we can write more maintainable code for 
an application cleanly separated according to the modules.

SO First create new mvc empty project.

Then Right clcik on solution explorer and click on add and select area.


area in mvc


Give Name.


Area

now add second Employee area. In your solution explorer show your model, view and controller in diffrent student and employee area.


Area in mvc

Now add Home Controller and in Index action add Index View for both student and employee area.

Now Run Project.(ctrl+F5).

For Teacher area follwing url localhost/Teachers/Home/Index
Hear Teachers is area name, Home is controller and Index is Action name.


Area in mvc

For Teacher area follwing url localhost/Students/Home/Index
Hear Students is area name, Home is controller and Index is Action name.


Area in mvc


Download Webgrid data in a pdf file with MVC5

INTRODUCTION


In this article, I am explain How to Download Webgrid data in a pdf file.

In previous post I explained display data In webgrid and delete multiple rows by using checkboxes. Now i will explain how to download that data in a pdf file.

So first check previous post and add Action Link in list.cshtml for Create pdf.


webgrid in mvc

 Create PDF : @Html.ActionLink("Create PDF", "CreatePdf", "Home")


Go to Solution Explorer > Right Click on References > Manage NuGet Packages...> Search with itextsharp text > Install this 2 dll



NuGet Manager


iTextSharp


Now Add CreatePdf Action into Home Controller and also add following namespace.


using iTextSharp.text;

using iTextSharp.text.pdf;

using System.IO;

using System.Web.Helpers;


Now Add CreatePdf Action code .


public FileStreamResult CreatePdf()

        {

            List all = new List();          

            all = db.Employees.ToList();            

            WebGrid grid = new WebGrid(source: all, canPage: false, canSort: false);

            string gridHtml = grid.GetHtml(

                    columns: grid.Columns(

                           grid.Column("Id", "EmployeeId"),

                           grid.Column("Name", "Employee Name"),

                           grid.Column("City", "City"),

                           grid.Column("MobileNo", "Mobile No")

                        )

                    ).ToString();

            string exportData = String.Format("{0}{1}", "", gridHtml);

            var bytes = System.Text.Encoding.UTF8.GetBytes(exportData);

            using (var input = new MemoryStream(bytes))

            {

                var output = new MemoryStream();

                var document = new iTextSharp.text.Document(PageSize.A4, 50, 50, 50, 50);

                var writer = PdfWriter.GetInstance(document, output);

                writer.CloseStream = false;

                document.Open();

                var xmlWorker = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();

                xmlWorker.ParseXHtml(writer, document, input, System.Text.Encoding.UTF8);

                document.Close();

                output.Position = 0;

                return new FileStreamResult(output, "application/pdf");

            }

        }


RUN APPLICATION(CTTL + F5).


Create Pdf in mvc

Click on Create PDF. and get PDF.


pdf in mvc





Display Data In Webgrid And Delete Multiple Rows By Using Checkboxes In MVC 5 Application

INTRODUCTION

In this article, I am explain How to Display Data In Webgrid And Delete Multiple Rows By Using Checkboxes In MVC 5 Application.

CREATE NEW PROJECT.

Go to File > New > Project > Select asp.net web application (with .NET Framework 4.5) > Enter Application Name > Click OK > Select Empty MVC Template > OK

Create Table.

DATA TABLE

Add Entity data model.
In previous articles I explained how to add entity data model. here

ADD A NEW CONTROLLER.

Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > MVC5 Controller- Empty > Enter Controller name Home > Add.

ADD NEW ACTION INTO YOUR CONTROLLER FOR DISPLAY DATA IN WEBGRID.

Here I have added "List" Action into "Home" Controller. write this following code. 

LIST ACTION

ADD VIEW FOR for the LIST ACTION

Right Click on Action Method (here right click on form action) > Add View... 

ADD VIEW

Rebuild solution before add view.

Write following code into List view.

LIST VIEW


ADD ANOTHER ACTION INTO YOUR CONTROLLER FOR DELETE MULTIPLE ROWS AT ONCE.

Here I have added "Delete" Action into "Home" Controller. 
write this following code .

DELETE ACTION

RUN APPLICATION(CTTL + F5).

WEBGRID

Dropdownlist control in MVC using HTML helpers

A dropdownlist in MVC is a collection of SelectListitem objects. Depending on your project requirement you may either hard code the values in code or retrieve them from a database table.

First here we show dropdownlist using hard coded value.

Write following code to your view.


DROPDOWNLIST

Now run application.(ctrl + F5).


DROPDOWNLIST

Now write code in Index action for retrieve data from database.

DROPDOWNLIST

Right click on Index action and Add View and write following code.

DROPDOWNLIST

Now run application.(ctrl + F5).

DROPDOWNLIST






Insert, Update, Delete data in MVC5 using EntityFramework

This tutorial will show you how you can perform insert, update, delete operation in an mvc application using entity framework. This is called CRUD functionality. This you can use in MVC3, MVC4, MVC5.

Here we use entity framework 6 with MVC5.

First of here is our sql table:


DATABASE TABLE

So for this tutorial first we will create a new empty mvc application in this we will add a ADO.NET entity data model in Model.

NEW ITEM

Select EF Designer from database.

DATA MODEL

Then select Server name and Database name.

CONNECTION PROPERTIES

Select Yes radio button and click on Next.

ENTITY DATA MODEL WIZARD

Select Entity Framework 6.x.

ENTITY FRAMEWORK 6

Then Select table.

DATABASE


Now MVCdb.edmx add in Model folder.

EDMX FILE


Now Add Employee Controller.

CONTROLLER


Choose MVC5 Controller with views, using Entity Framework.

MVC 5 CONTROLLER

Select Model class and DataContextClass and give Controller name Employee.

ADD CONTROLLER


so now, automatically create  Index,Create,Delete,Edit,Details Views and Controller actions.

EMPLOYEE CONTROLLER

Now Run Application. (ctrl+F5)

RUN APPLICATION

Clcik on Create New.

CREATE
Add data and Click on Create button.

EMPLOYEE






Views in MVC Application

A view requests information from the model that it uses to generate an output representation to the user.

First Add Home Controller in MVC Project.


ViewBag & ViewData is a mechanism to pass data from controller to view.



To pass data from controller to a view, it's always a good practice to use strongly typed view models.

We use "@" symbol to switch between html and C# code.

ADD VIEW

Enter View Name. and Click Add Button.

ADD VIEW

Now Create Index.cshtml file in View/Home Folder.

Write Following code into Home Controller.

INDEX ACTION

Now open Index.cshtml file. and write  following code.

INDEX VIEW

Now Run Project.(Ctrl+F5)

WELCOME