簡體   English   中英

如何在Visual Studio 2012中為ASP.NET MVC 4配置Microsoft.Owin.Security.Google?

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

嘗試在帶有Visual Studio 2012的ASP.NET MVC 4項目中使用Microsoft.Owin.Security.Google配置安裝程序並使用Google配置登錄。

我一直在關注這篇文章: http : //www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-登錄並檢查其他對象,它們都指向將代碼放置在ASP.NET MVC 4模板沒有的Startup.cs文件中。

Microsoft Owin庫僅適用於ASP.NET MVC 5嗎?

是的,使用ASP.NET MVC5可以輕松得多:

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")
            });

            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

            /* Google */
            var googleOptions = new GoogleOAuth2AuthenticationOptions
            {
                ClientId = "clientid",
                ClientSecret = "secret",
            };
            googleOptions.Scope.Add("https://www.googleapis.com/auth/userinfo.profile");
            googleOptions.Scope.Add("https://www.googleapis.com/auth/userinfo.email");
            app.UseGoogleAuthentication(googleOptions);

            /* Facebook */
            var facebookOptions = new FacebookAuthenticationOptions
            {
                AppId = "clientid",
                AppSecret = "secret",
                BackchannelHttpHandler = new FacebookBackChannelHandler(),
                UserInformationEndpoint = "https://graph.facebook.com/v2.4/me?fields=id,name,email,first_name,last_name,location",
            };

            facebookOptions.Scope.Add("email");
            app.UseFacebookAuthentication(facebookOptions);

            AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
        }
 }
}

FacebookBackChannelHandler

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);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM