【问题标题】:RestSharp calling WebAPI with Thinktecture AuthenticationConfigurationRestSharp 使用 Thinktecture AuthenticationConfiguration 调用 WebAPI
【发布时间】:2014-12-27 04:08:21
【问题描述】:

我在 MVC 应用程序中使用 Restsharp,试图调用受 Thinktecture IdentityModel AuthenticationConfiguration 保护的后端 MVC WebAPI。

MVC API 设置

我的 MVC API 测试设置如下:

private static void ConfigureAuth(HttpConfiguration config)
{
    var authConfig = new AuthenticationConfiguration
    {
        DefaultAuthenticationScheme = "Basic",
        EnableSessionToken = true,
        SendWwwAuthenticateResponseHeader = true,
        RequireSsl = false,
        ClaimsAuthenticationManager = new AddCustomClaims(),

        SessionToken = new SessionTokenConfiguration
        {
            EndpointAddress = "/token",
            SigningKey = Convert.ToBase64String(CryptoRandom.CreateRandomKey(32)),
            DefaultTokenLifetime = new TimeSpan(1, 0, 0)
        }
    };

    authConfig.AddBasicAuthentication((username, password) =>
    {
        return username == "admin" && password == "password";
    });

    config.MessageHandlers.Add(new AuthenticationHandler(authConfig));
}

private static void ConfigureCors(HttpConfiguration config)
{
    var corsConfig = new WebApiCorsConfiguration();
    config.MessageHandlers.Add(new CorsMessageHandler(corsConfig, config));

    corsConfig
        .ForAllOrigins()
        .AllowAllMethods()
        .AllowAllRequestHeaders(); 
}

Javascript 工作正常

我知道 100% 我使用 Restsharp 发送的令牌是正确的,并且可以使用等效的 json 调用(javascript 中使用的令牌与 Web MVC 控制器中使用的令牌相同,因为它存储在 Session 数组中):

var authToken = config.authToken,
baseUri = config.baseUri,
configureRequest = function (xhr) {
    xhr.setRequestHeader("Authorization", "Session " + authToken);
},
errorHandler = function (xhr, status, error) {
    if (xhr.status === 401 && config.onAuthFail) {
        config.onAuthFail(xhr, status, error);
    }
};

从我的 MVC Web 前端客户端应用调用 API - 此请求的授权已被拒绝

然后在我的 MVC 应用程序控制器操作中,我使用 RestSharp,如下所示:

public ActionResult Test()
{
    var token = Session[Constants.SessionTokenKey] as string;

    var client = new RestClient(new Uri("http://localhost:65104/"));

    var request = new RestRequest("contacts", Method.GET);
    string authHeader = System.Net.HttpRequestHeader.Authorization.ToString();
    request.AddHeader(authHeader, string.Format("Authorization Session {0}", token));

    var json = client.Execute(request);
    // break point here checking the status it has been denied

    return View("Index");
}

检查状态,返回"{\"message\":\"Authorization has been denied for this request.\"}"

我尝试使用带有 request.AddHeader(authHeader, string.Format("Authorization Session {0}", token));request.AddHeader(authHeader, string.Format("JWT {0}", token)); 的 Restsharp 请求方法添加令牌,但两种方式都拒绝相同的访问。

请问我做错了什么或者有什么建议可以去哪里看?

【问题讨论】:

    标签: asp.net-mvc asp.net-web-api asp.net-web-api2 restsharp thinktecture-ident-model


    【解决方案1】:

    看起来您的 JavaScript 代码和 RestSharp 请求代码不匹配。

    在 JS 中,你设置一个名称为 Authorization 的标题,并给它一个值 Session sometoken

    xhr.setRequestHeader("Authorization", "Session " + authToken);
    

    在 RestSharp 中,您分配一个名称为 Authorization 的标头,一个值 Authorization Session sometoken

    request.AddHeader(authHeader, string.Format("Authorization Session {0}", token));
    

    所以我建议将您的 RestSharp AddHeader 代码更改为:

    request.AddHeader(authHeader, string.Format("Session {0}", token));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      • 2015-04-20
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多