2012-08-01 78 views
2

我有一个运行ASP.Net Web API应用程序的Azure工作者角色。这受ACS保护。当我使用浏览器浏览网页api时,我受到ACS的挑战,无法对Google或LiveId进行身份验证。当我进行身份验证时,我可以看到数据。访问受Azure保护的Web API站点受到来自Metro应用程序的ACS的保护

我想从Win 8 Metro Style应用程序访问相同的API。我正在尝试使用WebAuthenticationBroker。

WebAuthenticationResult webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(
                 WebAuthenticationOptions.None, 
                 new Uri("https://xxxxx.accesscontrol.windows.net:443/v2/wsfederation?wa=wsignin1.0&wtrealm=http%3a%2f%2fyyyyy.cloudapp.net%2f"), 
                 new Uri("http://yyyyy.cloudapp.net/") 
                 ); 

当我启动应用程序,它挑战我既可以通过谷歌或LiveID的作为之前进行认证。这通过,我得到了一个成功的结果。

然后,我创建使用的HttpClient的API调用:

HttpClient client = new HttpClient(); 
    Uri _baseUri = new Uri("http://yyyyy.cloudapp.net/api/"); 
    client.BaseAddress = _baseUri; 

    var response = client.GetAsync("Values/").Result; 
    var responseBodyAsText = response.Content.ReadAsStringAsync().Result; 

    var ids = JsonConvert.DeserializeObject<IList<int>>(responseBodyAsText); 

这种类型的作品,这似乎是正确导航到URI,但回来的有效载荷是HTML问我登录而不是我期待的JSON数据。

我已经花了更多的时间在这方面比明智和已经用完了想法!任何人都可以帮忙吗?

+0

我有同样的问题。根据所使用的登录提供程序,响应数据不同。使用facebook,结果是一个很长的字符串,例如:/ v2/facebook?cx = cHI9d3NmZWRlcmF0aW9uJnJtPWh0并使用LiveID的简称:net/v2/wsfederation?wa = wsignin1.0。我也很想知道如何从这里继续 – 2012-09-18 16:34:40

+0

到目前为止,我已经发现,Facebook的url需要response_type = token来获取令牌。假设对其他提供商也是如此 – 2012-09-18 17:22:50

+0

response_type =谷歌代码 – 2012-09-18 17:23:55

回答

0

调用API时,您不应该在WebAuthenticationResult中重新使用令牌吗?

var webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, 
     new Uri("https://xxxxx.accesscontrol.windows.net:443/v2/wsfederation?wa=wsignin1.0&wtrealm=http%3a%2f%2fyyyyy.cloudapp.net%2f"), new Uri("http://yyyyy.cloudapp.net/")); 

var client = new HttpClient(); 
client.DefaultRequestHeaders.Authorization 
     = new AuthenticationHeaderValue("OAuth", webAuthenticationResult.ResponseData); 
+0

我如何从WebAuthenticationResponse获取令牌? – 2012-08-01 13:00:21

+0

从我理解的令牌应该在ResponseData属性 – 2012-08-01 13:22:41

+0

这不起作用 - 我回到ResponseData的所有内容是http://yyyyy.cloudapp.net/这有没有关系这是http不是https? – 2012-08-01 13:26:08

1

WebAuthenticationBroker只会继续浏览直到浏览器会加载URL在callbackUri参数中指定的一个。然后它会将最终的网址返回给您,包括任何查询参数。它从来没有真正向callbackUri发出请求。

您遇到的问题是,它ACS实现岗位令牌回返回URL(在ACS配置门户网站指定),这可能是你指定为callbackUri参数相同的URL WS联盟协议的AuthenticateAsync方法。

您需要做的是处理该帖子并将请求重定向到callbackUri,但包含令牌的查询字符串。

我使用的是WebApi和WIF,我有我的callbackUri指定为http://localhost:3949/api/federationcallback/end,并返回指定为http://localhost:3949/api/federationcallback的url。这使我可以让控制器处理帖子,然后使用URL中的令牌重定向。控制器看起来像这样。

public class FederationcallbackController : ApiController 
{ 
    public HttpResponseMessage Post() 
    { 
     var response = this.Request.CreateResponse(HttpStatusCode.Redirect); 
     response.Headers.Add("Location", "/api/federationcallback/end?acsToken=" + ExtractBootstrapToken()); 

     return response; 
    } 

    protected virtual string ExtractBootstrapToken() 
    { 
     return HttpContext.Current.User.BootstrapToken(); 
    } 
} 

然后我就可以得到这样的标记:

var authenticateResult = await WebAuthenticationBroker.AuthenticateAsync(
      WebAuthenticationOptions.None, 
      new Uri("https://xyz.accesscontrol.windows.net:443/v2/wsfederation?wa=wsignin1.0&wtrealm=http%3a%2f%2flocalhost:3949%2f"), 
      new Uri("http://localhost:3949/api/federationcallback/end")); 

var oauthToken = authenticateResult.ResponseData.Substring(authenticateResult.ResponseData.IndexOf("acsToken=", StringComparison.Ordinal) + 9); 
+0

bootstraptoken方法是什么? – 2012-09-27 07:10:44

+0

并且ACS是否知道localhost是我的ip而不是localhost作为ACS的本地主机? – 2012-09-27 07:37:47

+0

BootstrapToken方法是包含在wif.swt NuGet包中的扩展方法。我认为这是WIF的一部分,因此我之前没有提到它。 – 2012-09-27 12:32:17