0

我正在使用 OWIN 中间件使用基于 Web Api 令牌的身份验证;令牌已成功生成,但我无法对其进行解码;例如,我无法从中提取用户名和密码;这是我的配置我的启动代码

var oAuthAuthorizationServerOptions = new OAuthAuthorizationServerOptions
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/api/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = new SimpleAuthorizationServerProvider()
        };
        // Token Generation
        app.UseOAuthAuthorizationServer(oAuthAuthorizationServerOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

我用于发送令牌的代码是

static async Task RunAsync(JObject token)
    {
        using (var client = new HttpClient())
        {
            client.Timeout = new TimeSpan(1000000000000);
            client.BaseAddress = new Uri("http://localhost/SampleApp/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(token["token_type"].ToString(),
                token["access_token"].ToString());
  }}

我的验证码

var authenticationSchema = httpContext.Request.Headers["Authorization"];
            if (!String.IsNullOrWhiteSpace(authenticationSchema))
                authentication = AuthenticationHeaderValue.Parse(authenticationSchema);

if (authentication != null)
{
 var unencoded = Convert.FromBase64String(authentication.Parameter);
        var userpw = Encoding.GetEncoding("iso-8859-  1").GetString(unencoded);
        var creds = userpw.Split(':');
        return new Tuple<string, string>(creds[0], creds[1]);
}

and the code failed when trying to decode the code from base64 string note:- my sample token is 3K8vHKHA2ZsKfKbvzUbo4a2sat2JLzvvyxCZ0KSD6s1wUS3t3oDPXuQ89aTmGpsG4ZL8O0cr8M9EUeZGtdM6FBwR7gLFcLZkTaimFGKyyZMNce9trQavVTzs6gam6qach1rPTLv_gIYGgPmM-401PZsr89BIXw4acTpJL3KbXs8y7PQ-o-eTV2IA8euCVkqC02iEnAzmS0SwhBouISCC-HvcNpE2aNixg4JXEt8EslU you can see the attached for the exception在此处输入图像描述

4

1 回答 1

0

据我从代码中可以看出,访问令牌是直接发送到服务器的;但是您需要在客户端对访问令牌进行编码,例如:

client.DefaultRequestHeaders.Authorization = 
    new AuthenticationHeaderValue(token["token_type"].ToString(),
        Convert.ToBase64String(Encoding.GetEncoding("iso-8859-1").GetBytes(token["access_token"].ToString())));

然后您可以在服务器端从 base64 字符串转换访问令牌。您提供的访问令牌字符串值不是有效的 Base64 字符串,如异常消息中所示。

于 2015-08-17T10:23:25.893 回答