Tag: access token

如何从令牌中提取并获得声明?

我一直在寻找一个确切的答案,但似乎大多数答案是客户电话和超出范围。 问题:我已经拥有访问令牌访问令牌。 如何仅使用访问令牌使用c#代码获取声明? 我想:以下是相同的问题,但我认为没有答案。 如何从经过身份validation的SecurityToken中获取声明 如何从我的Oauth令牌中读取声明?

SignInAsync vs AuthenticateAsync

我终于通过JWT令牌认证工作获得了登录方法。 我在这里打电话 await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, ClaimsPrincipalFactory.CreatePrincipal(claims), authProps); 我也打过电话 await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme); 在示例中,我读到我只需要SignInAsync 。 所以我测试了它并删除了AuthenticateAsync 。 但是, User.Identity.IsAuthenticated仍然返回true 。 是否可以删除AuthenticateAsync ? 还是我还需要它? 它为什么存在? AuthenticateAsync的文档字符串仅表示用于validation的扩展方法

在ASP.NET WebApi 2中自定义承载令牌JSON结果

我演练了这个官方的ASP.NET教程 ,持票令牌发布如下JSON。 { “access_token”:”boQtj0SCGz2GFGz[…]”, “token_type”:”bearer”, “expires_in”:1209599, “userName”:”Alice”, “.issued”:”Mon, 14 Oct 2013 06:53:32 GMT”, “.expires”:”Mon, 28 Oct 2013 06:53:32 GMT” } 我想添加用户配置文件属性以及上述结果,以减少来自客户端的请求数。 示例如下…… { “access_token”:”boQtj0SCGz2GFGz[…]”, “token_type”:”bearer”, “expires_in”:1209599, “userName”:”Alice”, “.issued”:”Mon, 14 Oct 2013 06:53:32 GMT”, “.expires”:”Mon, 28 Oct 2013 06:53:32 GMT”, “Notifications”:35, “IsEventMember”:true, “Promotion”:324372 } 我使用的oauth提供程序来自默认的ASP.NET模板( ApplicationOAuthProvider.cs ), OAuthOption如下所示。 OAuthOptions = new OAuthAuthorizationServerOptions { TokenEndpointPath = new […]

如何在Windows应用程序C#.net中获取Google plus访问令牌

我正在准备一个Windows应用程序,因为我想使用oAuth2来访问来自google plus的信息。 但我对以下代码的要求不好。我可以在谷歌控制台中创建应用程序并获取应用程序访问的“代码”。 WebRequest request = WebRequest.Create( GoogleAuthenticationServer.Description.TokenEndpoint ); // You must use POST for the code exchange. request.Method = “POST”; // Create POST data. string postData = FormPostData(code); byte[] byteArray = Encoding.UTF8.`enter code here`GetBytes(postData); // Set up the POST request for the code exchange. request.ContentType = “application/x-www-form-urlencoded”; request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); […]

如何使用System.IdentityModel.Tokens.Jwt使用Google OAuth2兼容算法RSA SHA-256生成JWT?

我正在尝试创建一个JWT来使用System.IdentityModel.Tokens.Jwt在Google文档中描述的服务帐户进行授权。 我有以下代码: byte[] key = Convert.FromBase64String(“…”); var certificate = new X509Certificate2(key, “notasecret”); DateTime now = DateTime.UtcNow; TimeSpan span = now – UnixEpoch; Claim[] claims = { new Claim(“iss”, “email@developer.gserviceaccount.com”), new Claim(“scope”, “https://www.googleapis.com/auth/plus.me”), new Claim(“aud”, “https://accounts.google.com/o/oauth2/token”), new Claim(“iat”, span.TotalSeconds.ToString()), new Claim(“exp”, span.Add(TimeSpan.FromHours(1)).TotalSeconds.ToString()) }; JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler(); var descriptor = new SecurityTokenDescriptor { SigningCredentials = […]

JWT和Web API(JwtAuthForWebAPI?) – 寻找一个例子

我有一个Angular前面的Web API项目,我想使用JWT令牌来保护它。 我已经进行了用户/通过validation,所以我认为我只需要实现JWT部分。 我相信我已经确定了JwtAuthForWebAPI所以使用它的一个例子会很棒。 我假设任何未使用[Authorize]修饰的方法都会像往常一样运行,并且如果客户端传递的令牌不匹配,则[Authorize]修饰的任何方法都将为401。 我还不知道如何在初始身份validation时将令牌发送回客户端。 我正在尝试使用魔术字符串开始,所以我有这个代码: RegisterRoutes(GlobalConfiguration.Configuration.Routes); var builder = new SecurityTokenBuilder(); var jwtHandler = new JwtAuthenticationMessageHandler { AllowedAudience = “http://xxxx.com”, Issuer = “corp”, SigningToken = builder.CreateFromKey(Convert.ToBase64String(new byte[]{4,2,2,6})) }; GlobalConfiguration.Configuration.MessageHandlers.Add(jwtHandler); 但我不确定最初是如何回到客户端的。 我想我理解如何在客户端上处理这个问题,但是如果你还可以展示这种交互的Angular方面,还可以获得奖励积分。

使用适用于.NET的YouTube v3 Data API,如何获得刷新令牌?

我需要能够使用刷新令牌,以便在访问令牌过期后重新validation令牌。 如何使用C#v3 API执行此操作? 我查看了UserCredential类和AuthorizationCodeFlow类,没有任何内容向我跳出来。 我正在使用以下代码对其进行身份validation。 var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()). AuthorizeAsync(CancellationToken.None); if (result.Credential != null) { var service = new YouTubeService(new BaseClientService.Initializer { HttpClientInitializer = result.Credential, ApplicationName = “YouTube Upload Tool” }); } 这是我的AppFlowMetadata类。 public class AppFlowMetadata : FlowMetadata { private static readonly IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = […]

Google Calendar API – 错误请求(400)尝试交换访问令牌代码

获得Google的授权代码以访问用户的日历后,我现在尝试将其替换为访问令牌。 根据自己的文档: 实际请求可能如下所示: POST /o/oauth2/token HTTP/1.1 Host: accounts.google.com Content-Type: application/x-www-form-urlencoded code=4/v6xr77ewYqhvHSyW6UJ1w7jKwAzu& client_id=8819981768.apps.googleusercontent.com& client_secret={client_secret}& redirect_uri=https://oauth2-login-demo.appspot.com/code& grant_type=authorization_code 我尝试访问它的方法如下(C#): string url = “https://accounts.google.com/o/oauth2/token”; WebRequest request = HttpWebRequest.Create(url); request.Method = “POST”; request.ContentType = “application/x-www-form-urlencoded”; string body = “code=&\r\n” + “client_id=&\r\n” + “client_secret=&\r\n” + “redirect_uri=http://localhost:4300\r\n” + “grant_type=authorization_code&\r\n” ; byte[] bodyBytes = Encoding.ASCII.GetBytes(body); request.ContentLength = bodyBytes.Length ; Stream bodyStream = request.GetRequestStream(); […]

如何从.NET发布到Facebook页面墙

我创建了Facebook页面。 我没有应用程序密钥,也没有访问令牌。 我想从我的.NET桌面应用程序发布到此页面。 我该怎么做? 任何人都可以帮忙,我在哪里可以获得访问令牌? 我应该创建一个新的Facebook应用程序吗? 如果是,我如何授予此应用程序的权限以在页面的墙上发布? UPD1:我没有网站。 我需要将公司的新闻从.NET桌面应用程序发布到公司的Facebook页面。 我所拥有的只是Facebook页面帐户的登录/密码。 UPD2:我创建了Facebook应用程序。 使用AppID / SecretKey。 我可以获得访问令牌。 但是……我如何授予发布到页面墙的权限? (OAuthException) (#200) The user hasn’t authorized the application to perform this action