我意识到这个问题之前已经被问过,但是代码示例的方式很少,所以我再问一次,但至少有一点方向.
经过几个小时的搜索,我得出了以下部分实现.
namespace GoogleAnalyticsAPITest.Console
{
using System.Security.Cryptography.X509Certificates;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Analytics.v3;
using Google.Apis.Analytics.v3.Data;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
class Program
{
static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure();
string Scope = Google.Apis.Analytics.v3.AnalyticsService.Scopes.Analytics.ToString().ToLower();
string scopeUrl = "https://www.googleapis.com/auth/" + Scope;
const string ServiceAccountId = "nnnnnnnnnnn.apps.googleusercontent.com";
const string ServiceAccountUser = "nnnnnnnnnnn@developer.gserviceaccount.com";
AssertionFlowClient client = new AssertionFlowClient(
GoogleAuthenticationServer.Description, new X509Certificate2(@"7039572692013fc5deada350904f55bad2588a2a-privatekey.p12", "notasecret", X509KeyStorageFlags.Exportable))
{
Scope = scopeUrl,
ServiceAccountId = ServiceAccountId//,ServiceAccountUser = ServiceAccountUser
};
IAuthorizationState state = AssertionFlowClient.GetState(client);
AnalyticsService service = new AnalyticsService(authenticator);
string profileId = "ga:xxxxxxxx";
string …Run Code Online (Sandbox Code Playgroud) 我正在努力实现(或理解)JWT承载令牌认证的签名密钥.我希望有人可以帮助我或解释我的错误.
在过去的几周里,我抓了大量的教程,设法让一个自定义的Auth-Controller运行,它发出我的令牌并设法建立JWT承载认证来验证标题中的令牌.
有用.
我的问题是所有示例和教程要么生成随机或内存(发行者)签名密钥,要么使用硬编码的"密码"字符串或从某些配置文件中获取它们(在代码示例中查找"密码").
我的意思是验证设置(在StartUp.cs中):
//using hardcoded "password"
SecurityKey key = new SymmetricSecurityKey(System.Text.Encoding.ASCII.GetBytes("password"));
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = "MyIssuer",
ValidateAudience = true,
ValidAudience = "MyAudience",
ValidateLifetime = true,
IssuerSigningKey = key
}
});
Run Code Online (Sandbox Code Playgroud)
在创建令牌的AuthController中:
//using hardcoded password
var signingKey = new SymmetricSecurityKey(System.Text.Encoding.ASCII.GetBytes("password"));
SigningCredentials credentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256);
var jwt = new JwtSecurityToken // Create the JWT and write it to a string
(
issuer: _jwtTokenSettings.Issuer, …Run Code Online (Sandbox Code Playgroud) var rawData = Convert.FromBase64String(_signingKey);
var cng = CngKey.Import(rawData, CngKeyBlobFormat.Pkcs8PrivateBlob);
Run Code Online (Sandbox Code Playgroud)
我使用此代码从嵌入base64字符串中提取密钥.我在本地测试时工作正常,但是当我在azure上发布时,我得到以下异常:
WindowsCryptographicException: The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)
(再一次,我不是从任何文件中读取)我需要这个与苹果apns进行通信以进行推送通知,是否有任何解决方法?这只发生在免费服务计划上,如果我切换到它正在运行的基本计划.