在GitHub like this one上的一些Azure示例中,我们有一个使用ADAL访问受保护的Web API资源的示例。该工作是由一个try/catch寻找一个AdalException我应该在ActionFilter中测试AdalException吗?
保护我会这样总结代码:
try
{
//pseudo code... configure a client to use an access token..
var token = ADAL.AcquireTokenAsync...
var httpClient = new HttpClient(baseUri, token);
// use the token for querying some protected API
var result = //await HttpClient to return data...
return View(result);
}
catch (AdalException)
{
// do something important with the exception,
// e.g. return an error View w/login link
}
所以,我开始充实我的MVC控制器使用ADAL-ACCESS_TOKEN” d要求,我是否真的想要在每个控制器中进行这种尝试/捕捉业务?
创建一个ActionFilter
是否有意义?这段代码是由代码的启发,我看到在this Azure Sample
我的上下文: 我以一个相当均匀的一套脚手架MVC控制器,是在生成它们时的EntityFramework为中心的..但现在需要重新设置以访问我的Web API(通过我的新AutoRest客户端)
好想法。肯定会做#2,谢谢你的指点。 – bkwdesign
提到的示例不会执行自动重定向 - 而是它们会修改UX,以便为用户提供链接来触发身份验证过程。这个想法是,无法从给定的控制器执行调用并不一定意味着用户不能对应用程序执行任何其他操作。这种方法的一个很好的例子:klout.com – vibronet