view raw
Klaatu Verata Necto Klaatu Verata Necto - 7 months ago 69
ASP.NET (C#) Question

How to configure Microsoft.Owin.Security.Google for ASP.NET MVC 4 in Visual Studio 2012?

Trying to configure setup and configure login with Google using Microsoft.Owin.Security.Google in my ASP.NET MVC 4 project with Visual Studio 2012.

I've been following this article: and checked others as well and they all refer to placing code in Startup.cs file which ASP.NET MVC 4 template does not have.

Is Microsoft Owin library only available for ASP.NET MVC 5 then?


Yes it is a lot easier to accomplish this with ASP.NET MVC5:

in app_start/Startup.Auth.cs

using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.Google;
using Microsoft.Owin.Security.Facebook;
using Owin;
using System.Web.Helpers;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.Owin.Security;

namespace ui.web
    public partial class Startup
        public void ConfigureAuth(IAppBuilder app)

            /* Local logins */
            app.UseCookieAuthentication(new CookieAuthenticationOptions
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/login")


            /* Google */
            var googleOptions = new GoogleOAuth2AuthenticationOptions
                ClientId = "clientid",
                ClientSecret = "secret",

            /* Facebook */
            var facebookOptions = new FacebookAuthenticationOptions
                AppId = "clientid",
                AppSecret = "secret",
                BackchannelHttpHandler = new FacebookBackChannelHandler(),
                UserInformationEndpoint = ",name,email,first_name,last_name,location",


            AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;


using System;
using System.Net.Http;

namespace ui.web.infrastructure.auth
    public class FacebookBackChannelHandler : HttpClientHandler
        protected override async System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
            // Replace the RequestUri so it's not malformed
            if (!request.RequestUri.AbsolutePath.Contains("/oauth"))
                request.RequestUri = new Uri(request.RequestUri.AbsoluteUri.Replace("?access_token", "&access_token"));

            return await base.SendAsync(request, cancellationToken);