对于asp.net-mvc–如何在Google.Apis调用中使用ASP.NETMVCOwinAccessToken?感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍谷歌浏览器api调用,并为您
对于asp.net-mvc – 如何在Google.Apis调用中使用ASP.NET MVC Owin AccessToken?感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍谷歌浏览器api调用,并为您提供关于asp.net-core-mvc – 使用asp.net核心的google身份验证器、asp.net-mvc – .NET MVC4 Facebook,Google和Twitter登录、asp.net-mvc – ASP.Net MVC 5带范围的Google身份验证、asp.net-mvc – ASP.NET MVC不调用global.asax’EndRequest的有用信息。
本文目录一览:- asp.net-mvc – 如何在Google.Apis调用中使用ASP.NET MVC Owin AccessToken?(谷歌浏览器api调用)
- asp.net-core-mvc – 使用asp.net核心的google身份验证器
- asp.net-mvc – .NET MVC4 Facebook,Google和Twitter登录
- asp.net-mvc – ASP.Net MVC 5带范围的Google身份验证
- asp.net-mvc – ASP.NET MVC不调用global.asax’EndRequest
asp.net-mvc – 如何在Google.Apis调用中使用ASP.NET MVC Owin AccessToken?(谷歌浏览器api调用)
我的Google身份验证配置正常,我可以使用它成功登录我的应用程序.我将context.Accesstoken存储为身份验证回调中的声明(GoogleOAuth2AuthenticationProvider的OnAuthenticated“event”).
我的Startup.Auth.cs配置(app.UseGoogleAuthentication(ConfigureGooglePlus()))
private GoogleOAuth2Authenticationoptions ConfigureGooglePlus() { var goolePlusOptions = new GoogleOAuth2Authenticationoptions() { ClientId = "Xxxxxxx.apps.googleusercontent.com",ClientSecret = "YYYYYYzzzzzz",Provider = new GoogleOAuth2AuthenticationProvider() { OnAuthenticated = context => { context.Identity.AddClaim(new System.Security.Claims.Claim("Google_Accesstoken",context.Accesstoken)); return Task.Fromresult(0); } },SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie }; goolePlusOptions.Scope.Add("https://www.googleapis.com/auth/plus.login"); goolePlusOptions.Scope.Add("https://www.googleapis.com/auth/userinfo.email"); return goolePlusOptions;
}
抛出异常的代码(Execute()方法)
var externalIdentity = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie); var accesstokenClaim = externalIdentity.FindAll(loginProvider + "_Accesstoken").First(); var secrets = new ClientSecrets() { ClientId = "Xxxxxxx.apps.googleusercontent.com",ClientSecret = "YYYYYYzzzzzz" }; IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = secrets,Scopes = new[] { PlusService.Scope.PlusLogin,PlusService.Scope.UserinfoEmail } }); UserCredential credential = new UserCredential(flow,"me",new TokenResponse() { Accesstoken = accesstokenClaim.Value }); var ps = new PlusService( new BaseClientService.Initializer() { ApplicationName = "My App Name",HttpClientinitializer = credential }); var k = ps.People.List("me",PeopleResource.ListRequest.CollectionEnum.Visible).Execute();
有没有其他方法来获取原始的Accesstoken或刷新它而不通过整个身份验证过程(用户已经过身份验证)?
我需要查询一些GooglePlus个人资料数据,例如Givenname,familyName,gender,个人资料图片和个人资料网址.
解决方法
我只需将Accesstype =“offline”添加到GoogleOAuth2Authenticationoptions并保存一些额外的信息,以便在需要时创建一个新的TokenResponse实例.
Google身份验证选项
private GoogleOAuth2Authenticationoptions ConfigureGooglePlus() { var goolePlusOptions = new GoogleOAuth2Authenticationoptions() { Accesstype = "offline",ClientId = "Xxxxxxx.apps.googleusercontent.com",ClientSecret = "Yyyyyyyyyy",Provider = new GoogleOAuth2AuthenticationProvider() { OnAuthenticated = context => { context.Identity.AddClaim(new System.Security.Claims.Claim("Google_Accesstoken",context.Accesstoken)); if (context.RefreshToken != null) { context.Identity.AddClaim(new Claim("GoogleRefreshToken",context.RefreshToken)); } context.Identity.AddClaim(new Claim("GoogleUserId",context.Id)); context.Identity.AddClaim(new Claim("GoogletokenIssuedAt",DateTime.Now.ToBinary().ToString())); var expiresInSec = (long)(context.ExpiresIn.Value.TotalSeconds); context.Identity.AddClaim(new Claim("GoogletokenExpiresIn",expiresInSec.ToString())); return Task.Fromresult(0); } },SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie }; goolePlusOptions.Scope.Add("https://www.googleapis.com/auth/plus.login"); goolePlusOptions.Scope.Add("https://www.googleapis.com/auth/plus.me"); goolePlusOptions.Scope.Add("https://www.googleapis.com/auth/userinfo.email"); return goolePlusOptions; }
如何检索凭证(使用“存储”作为声明的令牌信息)
private async Task<UserCredential> GetCredentialForApiAsync() { var initializer = new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = new ClientSecrets { ClientId = "Xxxxxxxxx.apps.googleusercontent.com",ClientSecret = "YYyyyyyyyyy",},Scopes = new[] { "https://www.googleapis.com/auth/plus.login","https://www.googleapis.com/auth/plus.me","https://www.googleapis.com/auth/userinfo.email" } }; var flow = new GoogleAuthorizationCodeFlow(initializer); var identity = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ApplicationCookie); var userId = identity.FindFirstValue("GoogleUserId"); var token = new TokenResponse() { Accesstoken = identity.FindFirstValue("Google_Accesstoken"),RefreshToken = identity.FindFirstValue("GoogleRefreshToken"),Issued = DateTime.FromBinary(long.Parse(identity.FindFirstValue("GoogletokenIssuedAt"))),ExpiresInSeconds = long.Parse(identity.FindFirstValue("GoogletokenExpiresIn")),}; return new UserCredential(flow,userId,token); }
asp.net-core-mvc – 使用asp.net核心的google身份验证器
找到一个样本.
Sample Google Authenticator using asp.net
但是在使用asp.net核心时会有很多变化.
解决方法
https://github.com/damirkusar/AspNetCore.Totp
它的工作方式与GoogleAuthenticator完全相同,请查看Tests项目的实现(非常简单).
您只需编写几行来获取qurcode并验证pin代码:
using AspNetCore.Totp; ... // To generate the qrcode/setup key var totpSetupGenerator = new TotpSetupGenerator(); var totpSetup = totpSetupGenerator.Generate("You app name here","The username","YourSuperSecretKeyHere",300,300); string qrCodeImageUrl = totpSetup.QrCodeImage; string manualEntrySetupCode = totpSetup.ManualSetupKey; // To validate the pin after user input (where pin is an int variable) var totpValidator = new TotpValidator(); bool isCorrectPIN = totpValidator.Validate("YourSuperSecretKeyHere",pin);
asp.net-mvc – .NET MVC4 Facebook,Google和Twitter登录
我下载了DotNetopenAuth 4.1,但我在网上找不到任何关于如何将它与我的应用程序集成的示例,并在我的视图中有一个提供程序的登录表单.
谢谢
解决方法
只需生成一个新的MVC4应用程序,选择互联网模板,运行它并使用您的Facebook ID登录.
asp.net-mvc – ASP.Net MVC 5带范围的Google身份验证
当我在没有任何范围的情况下设置传入GoogleOauth2Authenticationoptions时,我就能够成功登录.
var googlePlusOptions = new GoogleOAuth2Authenticationoptions { ClientId = googleClientId,ClientSecret = googleClientSecret,SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie,Provider = new GoogleOAuth2AuthenticationProvider() { OnAuthenticated = async ctx => { ctx.Identity.AddClaim(new Claim("urn:tokens:googleplus:accesstoken",ctx.Accesstoken)); } },}; app.UseGoogleAuthentication(googlePlusOptions);
然后,此调用将返回一个设置了所有属性的ExternalLoginInfo对象
ExternalLoginInfo loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
当我添加任何范围时,我没有得到任何返回的登录信息.它只是空的.
var googlePlusOptions = new GoogleOAuth2Authenticationoptions { ClientId = googleClientId,}; googlePlusOptions.Scope.Add(YouTubeService.Scope.Youtube); app.UseGoogleAuthentication(googlePlusOptions);
然后调用获取外部信息只返回null.
ExternalLoginInfo loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
在Google开发控制台中,我启用了以下API.
> Analytics API
> BigQuery API
> Google Cloud sql
> Google云端存储
> Google云端存储JSON API
> Google Api
> Google Domains API
> Identity Toolkit API
> YouTube Analytics API
> YouTube Data API v3
关于向选项添加范围的一些事情是打破GetExternalLoginInfoAsync.
解决方法
OWIN中间件(3.0.0)……
我注意到fiddler默认情况下会将以下范围发送到accounts.google.com:
scope=openid%20profile%20email
如果您通过GoogleOAuth2Authenticationoptions.Scope.Add(…)添加自己的范围,则范围变为:
scope=YOUR_ScopES_ONLY
因此,您还需要添加默认范围(或者至少,这为我解决了问题):
var googlePlusOptions = new GoogleOAuth2Authenticationoptions { ... }; // default scopes googlePlusOptions.Scope.Add("openid"); googlePlusOptions.Scope.Add("profile"); googlePlusOptions.Scope.Add("email"); // additional scope(s) googlePlusOptions.Scope.Add("https://www.googleapis.com/auth/youtube.readonly");
asp.net-mvc – ASP.NET MVC不调用global.asax’EndRequest
我更改了在创建新项目时生成的Application_Start()进行测试:
protected void Application_Start() { EndRequest += (s,e) => { Console.Write("fghfgh"); }; RegisterRoutes(RouteTable.Routes); }
lambda不会被调用.任何想法为什么?
编辑:
我看到他们在SharpArch [http://code.google.com/p/sharp-architecture/],它在那里工作吗?
不,我不想使用HttpModule.
EDIT2:
我发现唯一的解决方法是使用Application_EndRequest与global.asax的私有静态成员结合:
private static WebSessionStorage wss; protected void Application_Start() { //... wss = new WebSessionStorage(this); //... } protected void Application_EndRequest(object sender,EventArgs e) { wss.EndRequest(sender,e); }
wss必须是私有的,因为它似乎是使用不同的实例对象(这)调用Application_EndRequest.这也可能是我的事件(如开头所述)不被调用的原因.
解决方法
protected void Application_EndRequest(object sender,EventArgs e) { }
这样可以预期.不知道事件.
关于asp.net-mvc – 如何在Google.Apis调用中使用ASP.NET MVC Owin AccessToken?和谷歌浏览器api调用的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于asp.net-core-mvc – 使用asp.net核心的google身份验证器、asp.net-mvc – .NET MVC4 Facebook,Google和Twitter登录、asp.net-mvc – ASP.Net MVC 5带范围的Google身份验证、asp.net-mvc – ASP.NET MVC不调用global.asax’EndRequest的相关信息,请在本站寻找。
本文标签: