如何在 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?
尝试在我的 ASP.NET MVC 4 项目中使用 Microsoft.Owin.Security.Google 使用 Google 配置设置和配置登录 Visual Studio 2012.
我一直在关注这篇文章:
http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on 并检查了其他人,他们都提到将代码放在 Startup.cs 文件中,ASP.NET MVC 4 模板没有。
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);
}
}
}
尝试在我的 ASP.NET MVC 4 项目中使用 Microsoft.Owin.Security.Google 使用 Google 配置设置和配置登录 Visual Studio 2012.
我一直在关注这篇文章: http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on 并检查了其他人,他们都提到将代码放在 Startup.cs 文件中,ASP.NET MVC 4 模板没有。
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);
}
}
}