Bra*_*kin 24 asp.net-mvc oauth google-authentication owin asp.net-identity
我目前正在升级我的Google登录流程,以便在他们的OpenID登录方法之前使用OAuth.
到目前为止,我已经确定的步骤是我已将Microsoft.Owin.Security.Google软件包升级到版本2.1.0,因为此版本包含在UseGoogleAuthentication方法中包含选项的功能.
我试图在链接中使用Alex Wheat的解决方案: 从外部身份验证提供程序获取MVC5框架OAuth/OWin身份提供者的ExtraData
Startup.Auth.cs中的代码(也包括Facebook身份验证)来自于:
var facebookAuthenticationOptions = new FacebookAuthenticationOptions()
{
AppId = "MYAPPID",
AppSecret = "MYSECRET"
};
facebookAuthenticationOptions.Scope.Add("email");
app.UseFacebookAuthentication(facebookAuthenticationOptions);
app.UseGoogleAuthentication();
Run Code Online (Sandbox Code Playgroud)
对此:
var facebookAuthenticationOptions = new FacebookAuthenticationOptions()
{
AppId = "MYAPPID",
AppSecret = "MYSECRET"
};
facebookAuthenticationOptions.Scope.Add("email");
app.UseFacebookAuthentication(facebookAuthenticationOptions);
var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
{
ClientId = "MYCLIENTID",
ClientSecret = "MYSECRET",
CallbackPath = new PathString("/en/Account/ExternalLoginCallback"),
Provider = new GoogleOAuth2AuthenticationProvider()
{
}
};
app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);
Run Code Online (Sandbox Code Playgroud)
在我向Google身份验证添加选项后,我的应用不允许为Google或Facebook调用ExternalLoginCallback操作(不更改Facebook代码,但问题仍会影响它).
在前端,单击外部登录按钮后,页面将我重定向到下面的链接并返回一个空白屏幕
https ....../en/Account/ExternalLoginCallback #__ = _(在=符号之前实际上只有一个下划线,如果我在地址栏上显示它,则SO语法将其删除).
对于Facebook和
HTTPS ....../EN /帐号/ ExternalLoginCallback
对于谷歌.它没有像往常那样点击下面的控制器方法(我试图在这个函数中放置调试断点,并且当有google身份验证选项时它永远不会停止.
// GET: /Account/ExternalLoginCallback
[AllowAnonymous]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
Run Code Online (Sandbox Code Playgroud)
如果我从Google身份验证中删除身份验证选项,它只会恢复为旧的OpenID登录并再次正常工作.
我错过了一些简单的东西吗?或者Owin.Security.Google图书馆内是否有一些不良事件导致了这个问题?
Ale*_*ard 20
为简单起见,我使用带有身份验证的默认ASP.NET MVC 5模板,但希望可以针对不同的用例修改此模板.
StartupAuth.cs
不要自定义重定向路径.无论如何它被/ signin-google取代,我试图绕过它会导致"无声"(不在调试器中)内部服务器500错误.
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
ClientId = "whatevs.apps.googleusercontent.com",
ClientSecret = "whatevs_secrut",
Provider = new GoogleOAuth2AuthenticationProvider()
});
Run Code Online (Sandbox Code Playgroud)
确保添加http://whatever.com/signin-google到https://console.developers.google.com/在您APIs & auth> Credentials> Redirect URIs部分.
RouteConfig.cs
添加路由到路由的永久重定向控制器操作.永久重定向是唯一足够的.仅直接指向回调URL是不够的.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Google API Sign-in",
url: "signin-google",
defaults: new { controller = "Account", action = "ExternalLoginCallbackRedirect" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Run Code Online (Sandbox Code Playgroud)
AccountController.cs
永久重定向到内置回调方法,你应该没问题.
[AllowAnonymous]
public ActionResult ExternalLoginCallbackRedirect(string returnUrl)
{
return RedirectPermanent("/Account/ExternalLoginCallback");
}
Run Code Online (Sandbox Code Playgroud)
模板项目已发布在GitHub上以供参考:https: //github.com/Pritchard/Test-AspNetGoogleOAuth2Authentication
Suh*_*shi 15
试试这个
var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
{
ClientId = "MYCLIENTID",
ClientSecret = "MYSECRET",
};
app.UseGoogleAuthentication(googleOAuth2AuthenticationOptions);
Run Code Online (Sandbox Code Playgroud)
这对我有用
| 归档时间: |
|
| 查看次数: |
34359 次 |
| 最近记录: |