当我从Azure网站调用Google API时,我得到502-Web服务器在充当网关或代理服务器时收到无效响应。确切的代码可以在我的本地机器和Azure虚拟机上工作。
代码只是从谷歌用户id 中获取显示名称
private string GetUserDetails(string userId)
{
var serviceAccountEmail = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com";
var certFile = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/googlekey.p12");
var certificate = new X509Certificate2(certFile, "notasecret", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { PlusService.Scope.PlusMe }
}.FromCertificate(certificate));
var service = new PlusService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Bayfront"
});
var request = service.People.Get(userId);
var person = request.Execute();
return person.DisplayName;
}
这是在WebApi项目中调用的,但我已将其提取到一个单独的网页asp.net web表单中,网址为http://testgplus.azurewebsites.net/
我还尝试了一个带有ApiKey的简单REST客户端,而不是使用上面的。同样,这在虚拟机上有效,但在网站上无效,在那里我得到了403 Forbidden。我已经添加了该网站的IP地址&虚拟机到谷歌开发者控制台。
private string GetUserDetails2(string userId)
{
var client = new RestClient("https://www.googleapis.com/plus/v1/people/" + userId);
var request = new RestRequest(Method.GET);
request.AddParameter("key", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
var response = client.Execute(request);
if (response.StatusCode == HttpStatusCode.OK)
{
dynamic result = Newtonsoft.Json.JsonConvert.DeserializeObject(response.Content);
return result["name"]["givenName"];
}
return response.StatusCode.ToString();
}
看起来我无法为Azure网站调用外部web服务。我看到了一些类似的问题,例如502在azure';网站';,但这些建议都没有奏效。有人知道可能的原因或解决方案吗?
我之前看过你的问题,但没有注意到解决方案。。。我现在也有了。。生成证书时添加:
var certificate = new X509Certificate2(p12Path, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
//(notice the X509KeyStorageFlags.MachineKeySet |)
。你好Colin Mierowsky
在Application_Start或WebApiConfig Register方法中,您在哪里创建证书?
这个代码在哪里使用?
makecert -r -n "CN=abdullahsargin.com, E=sargin48@gmail.com" -sky exchange -b 11/01/2015 -pe -sv myhost.pvk myhost.cer
pvk2pfx -pvk myhost.pvk -spc myhost.cer -pfx myhost.pfx -po Test.123
在全球.asax应用程序启动
try
{
var certFile = Server.MapPath("~/App_Data/myhost.pfx");
var cert = new X509Certificate2(certFile, "Test.123",
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
}
catch (Exception exc)
{
_tools.LogError(exc);
}
这个方法在本地运行成功,但在azure中得到502关于这个代码,我一行一行地测试这个方法
var code = await _userManager.GeneratePasswordResetTokenAsync(user.Id);
完成该方法
[HttpGet, AllowAnonymous]
public async Task<HttpResponseMessage> ForgotPassword([FromUri] ForgotPasswordViewModel model)
{
try
{
var code = await _userManager.GeneratePasswordResetTokenAsync(user.Id);
return Request.CreateResponse(HttpStatusCode.OK, new { model = user });
var url = "http://abdullahsargin.com#/account/resetPassword/" + user.Id + "/" + code;
await _userManager.SendEmailAsync(user.Id, "Reset Password",
"Please reset your password by clicking here: <a href="" + url + "">link</a>");
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (Exception exc)
{
MyTools.LogError(exc.GetBaseException());
return Request.CreateResponse(HttpStatusCode.BadRequest, exc.GetBaseException());
}
}
我在这个页面上找到了我的解决方案
ASP.NET标识:在Azure网站上使用GeneratePasswordResetToken
对于我的解决方案
public UserManager() : base(new UserStore<ApplicationUser>(new MyDbContext()))
{
// other setup
this.UserTokenProvider = new TotpSecurityStampBasedTokenProvider<ApplicationUser, string>();
}