C# 如何使用刷新令牌续订访问令牌?
我正在将ASP.NET MVC 5与OWIN一起使用 我做了很多研究,还没有发现如何使用刷新令牌更新访问令牌 我的场景是:用户第一次访问我的应用程序时,他或她将访问权限授予我读取API返回的刷新令牌的帐户。当用户返回我的应用程序时,我需要根据“刷新令牌”刷新访问令牌 谁能提供一些代码 以下是我迄今为止所取得的成就: Startup.Auth.cs:C# 如何使用刷新令牌续订访问令牌?,c#,oauth-2.0,asp.net-mvc-5,google-oauth,owin,C#,Oauth 2.0,Asp.net Mvc 5,Google Oauth,Owin,我正在将ASP.NET MVC 5与OWIN一起使用 我做了很多研究,还没有发现如何使用刷新令牌更新访问令牌 我的场景是:用户第一次访问我的应用程序时,他或她将访问权限授予我读取API返回的刷新令牌的帐户。当用户返回我的应用程序时,我需要根据“刷新令牌”刷新访问令牌 谁能提供一些代码 以下是我迄今为止所取得的成就: Startup.Auth.cs: var googleOAuth2AuthenticationOptions = new GoogleOAuth2Authentication
var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
{
Caption = "Google+",
ClientId = Parameters.Instance.Authentication.oAuth.GooglePlus.ClientId,
ClientSecret = Parameters.Instance.Authentication.oAuth.GooglePlus.ClientSecret,
CallbackPath = new PathString("/oauth-login-return"),
Provider = new GoogleOAuth2AuthenticationProvider
{
OnAuthenticated = async context =>
{
context.Identity.AddClaim(new Claim(ClaimTypes.Name, context.Identity.FindFirstValue(ClaimTypes.Name)));
context.Identity.AddClaim(new Claim(ClaimTypes.Email, context.Identity.FindFirstValue(ClaimTypes.Email)));
context.Identity.AddClaim(new Claim("picture", context.User.GetValue("picture").ToString()));
context.Identity.AddClaim(new Claim("profile", context.User.GetValue("profile").ToString()));
context.Identity.AddClaim(
new Claim(Parameters.Instance.Authentication.oAuth.GooglePlus.AccessTokenClaimType,
context.AccessToken));
}
}
};
googleOAuth2AuthenticationOptions.Scope.Add("https://www.googleapis.com/auth/plus.login");
googleOAuth2AuthenticationOptions.Scope.Add("https://www.googleapis.com/auth/userinfo.email");
[HttpPost]
[AllowAnonymous]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
RedirectIfAuthenticated();
return new ChallengeResult(provider, Url.Content("~/oauth-login-callback"));
}
[ActionName("oauth-login-back")]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
}
// Used for XSRF protection when adding external logins
private const string XsrfKey = "XsrfId";
private IAuthenticationManager AuthenticationManager
{
get
{
return HttpContext.GetOwinContext().Authentication;
}
}
private class ChallengeResult : HttpUnauthorizedResult
{
public ChallengeResult(string provider, string redirectUri)
: this(provider, redirectUri, null)
{
}
private ChallengeResult(string provider, string redirectUri, string userId)
{
LoginProvider = provider;
RedirectUri = redirectUri;
UserId = userId;
}
private string LoginProvider { get; set; }
private string RedirectUri { get; set; }
private string UserId { get; set; }
public override void ExecuteResult(ControllerContext context)
{
var properties = new AuthenticationProperties { RedirectUri = RedirectUri };
if (UserId != null)
{
properties.Dictionary[XsrfKey] = UserId;
}
context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
}
}
认证控制器:
var googleOAuth2AuthenticationOptions = new GoogleOAuth2AuthenticationOptions
{
Caption = "Google+",
ClientId = Parameters.Instance.Authentication.oAuth.GooglePlus.ClientId,
ClientSecret = Parameters.Instance.Authentication.oAuth.GooglePlus.ClientSecret,
CallbackPath = new PathString("/oauth-login-return"),
Provider = new GoogleOAuth2AuthenticationProvider
{
OnAuthenticated = async context =>
{
context.Identity.AddClaim(new Claim(ClaimTypes.Name, context.Identity.FindFirstValue(ClaimTypes.Name)));
context.Identity.AddClaim(new Claim(ClaimTypes.Email, context.Identity.FindFirstValue(ClaimTypes.Email)));
context.Identity.AddClaim(new Claim("picture", context.User.GetValue("picture").ToString()));
context.Identity.AddClaim(new Claim("profile", context.User.GetValue("profile").ToString()));
context.Identity.AddClaim(
new Claim(Parameters.Instance.Authentication.oAuth.GooglePlus.AccessTokenClaimType,
context.AccessToken));
}
}
};
googleOAuth2AuthenticationOptions.Scope.Add("https://www.googleapis.com/auth/plus.login");
googleOAuth2AuthenticationOptions.Scope.Add("https://www.googleapis.com/auth/userinfo.email");
[HttpPost]
[AllowAnonymous]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
RedirectIfAuthenticated();
return new ChallengeResult(provider, Url.Content("~/oauth-login-callback"));
}
[ActionName("oauth-login-back")]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
}
// Used for XSRF protection when adding external logins
private const string XsrfKey = "XsrfId";
private IAuthenticationManager AuthenticationManager
{
get
{
return HttpContext.GetOwinContext().Authentication;
}
}
private class ChallengeResult : HttpUnauthorizedResult
{
public ChallengeResult(string provider, string redirectUri)
: this(provider, redirectUri, null)
{
}
private ChallengeResult(string provider, string redirectUri, string userId)
{
LoginProvider = provider;
RedirectUri = redirectUri;
UserId = userId;
}
private string LoginProvider { get; set; }
private string RedirectUri { get; set; }
private string UserId { get; set; }
public override void ExecuteResult(ControllerContext context)
{
var properties = new AuthenticationProperties { RedirectUri = RedirectUri };
if (UserId != null)
{
properties.Dictionary[XsrfKey] = UserId;
}
context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
}
}
[HttpPost]
[异名]
公共操作结果外部登录(字符串提供程序、字符串返回URL)
{
重定向已验证();
返回新的ChallengeResult(provider,Url.Content(“~/oauth登录回调”);
}
[ActionName(“oauth登录返回”)]
公共异步任务ExternalLoginCallback(字符串返回URL)
{
}
//用于添加外部登录时的XSRF保护
私有常量字符串XsrfKey=“XsrfId”;
专用IAAuthenticationManager AuthenticationManager
{
得到
{
返回HttpContext.GetOwinContext().Authentication;
}
}
私有类质询结果:HttpUnauthorizedResult
{
公共ChallengeResult(字符串提供程序、字符串重定向URI)
:此(提供程序、重定向URI、null)
{
}
私有ChallengeResult(字符串提供程序、字符串重定向URI、字符串用户ID)
{
LoginProvider=提供者;
重定向URI=重定向URI;
UserId=UserId;
}
私有字符串登录提供程序{get;set;}
私有字符串重定向URI{get;set;}
私有字符串用户标识{get;set;}
公共覆盖无效ExecuteSult(ControllerContext上下文)
{
var properties=newauthenticationproperties{RedirectUri=RedirectUri};
if(UserId!=null)
{
properties.Dictionary[XsrfKey]=UserId;
}
context.HttpContext.GetOwinContext().Authentication.Challenge(属性,LoginProvider);
}
}
这个问题一点也不重复。我希望这有助于其他人不要像我这样浪费时间
在将近4天之后,我发现了如何使用OWIN在googleapi中获取新的访问令牌
我将发布解决方案,但首先,我必须说,帮助我开始了解错误的线索是为Katana项目设置调试符号。请参阅此链接:
此图显示如何配置调试符号服务器。
这一个显示了正在加载的Katana调试符号。
在那之后,我发现我的问题是谷歌API返回403:禁止
“未配置访问权限。请使用Google开发者控制台激活项目的API”
然后,在这篇文章中发现堆栈溢出:
更具体地说,这个答案是:
之后,我去了谷歌开发者控制台,设置了谷歌+API
然后,沃拉!成功了
现在,代码使用刷新令牌获取一个新的访问令牌(我还没有找到使用OWINAPI实现这一点的任何方法)
重要提示2:一旦获得刷新令牌,必须将其存储在某个安全的源中。Google API不会向您发布新的刷新令牌,除非您在调用该行之前将“approval\u prompt”设置为“force”(使用相同的方法):
我还建议看一下:
过去两天我一直在琢磨如何更新访问令牌。答案发布在另一个帖子中:
您是如何获得OAuth承载令牌的?@ErikPhilips我插入了一些代码摘录。请检查一下。可能是@erikphilips的重复。对不起,erik,但我在阅读了该链接后没有理解要点。我应该建立一个oauth身份验证提供者,但我在GOOGLE中使用OW吗?谢谢。@ErikPhilips,请检查我的答案。
context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);