Getting the email from external providers Google and Facebook during account association step in a default MVC5 app

PLEASE SEE UPDATES AT THE BOTTOM OF THIS POST! The following works for me for Facebook: StartupAuth.cs: var facebookAuthenticationOptions = new FacebookAuthenticationOptions() { AppId = “x”, AppSecret = “y” }; facebookAuthenticationOptions.Scope.Add(“email”); app.UseFacebookAuthentication(facebookAuthenticationOptions); ExternalLoginCallback method: var externalIdentity = HttpContext.GetOwinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie); var emailClaim = externalIdentity.Result.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email); var email = emailClaim.Value; And for Google: StartupAuth.cs app.UseGoogleAuthentication(); … Read more

How to get JSON object from Razor Model object in javascript

You could use the following: var json = @Html.Raw(Json.Encode(@Model.CollegeInformationlist)); This would output the following (without seeing your model I’ve only included one field): <script> var json = [{“State”:”a state”}]; </script> Working Fiddle AspNetCore AspNetCore uses Json.Serialize intead of Json.Encode var json = @Html.Raw(Json.Serialize(@Model.CollegeInformationlist)); MVC 5/6 You can use Newtonsoft for this: @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model, Newtonsoft.Json.Formatting.Indented)) This gives … Read more

Visual Studio 2013 – No Visual Basic/Visual C# Web Templates Installed

I think that “Re-install Visual Studio from scratch” is not a solution. I have faced with the described problem and found much faster way to fix it: First of all, try to repair Visual Studio installation (in “Control Panel\Programs\Programs and Features” find your Visual Studio, right-click and select “Repair”). Reboot after (!). Check if template … Read more

How to create custom scaffold templates in ASP.NET MVC5?

First, it looks like you have Visual Studio 2013 and 2012 both installed on your computer. I tried looking up the path you provided, I couldn’t find it. On your path it looks like you’re trying to use MVC4 templates. Here is my path: C:\Program Files (x86)\Microsoft Visual Studio 12.0\ Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates Below is how I … Read more

VS2013 does not compile ASP.NET MVC5 views

Right click your web project in the Solution Explorer. Click Unload Project. Right click the project and click Edit <projname>.csproj. Make sure you have this element (add it if it doesn’t exist). <Project> <PropertyGroup> <MvcBuildViews>true</MvcBuildViews> </PropertyGroup> </Project> Scroll down to the bottom. You should see some comment “To modify your build process, add your task … Read more

How to inject UserManager & SignInManager

Prerequisites Start a new MVC5 application using the MVC template. This will install all the necessary dependencies as well as deploy the Startup.Auth.cs file which contains the bootstrap code for Microsoft.AspNet.Identity (it als includes the all the references for Microsoft.AspNet.Identity). Install the following packages and update them to the latest afterwards. Install-Package Ninject Install-Package Ninject.MVC5 … Read more

Login page on different domain

public class Startup { public void Configuration(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationMode = AuthenticationMode.Active, LoginPath = new PathString(“/account/login”), LogoutPath = new PathString(“/account/logout”), Provider = new CookieAuthenticationProvider { OnApplyRedirect = ApplyRedirect }, }); } private static void ApplyRedirect(CookieApplyRedirectContext context) { Uri absoluteUri; if (Uri.TryCreate(context.RedirectUri, UriKind.Absolute, out absoluteUri)) { var path = PathString.FromUriComponent(absoluteUri); if (path == … Read more