我正在MVC中编写API服务(没有视图,只是API),我想使用通过client_credentials流程(2-legged OAuth)获取的OAuth 2.0令牌.我在Azure管理门户中创建了一个ActiveDirectory应用程序,并成功获得了一个不记名令牌(请参阅底部Postman的截图).
然后我安装了Microsoft.Owin.Security.ActiveDirectorynuget包,创建了一个Owin启动类并在其中编写了以下代码:
public class OwinStartup
{
public void Configuration(IAppBuilder app)
{
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
var myoptions = new WindowsAzureActiveDirectoryBearerAuthenticationOptions();
myoptions.Audience = // my App ID
myoptions.Tenant = // my tenant
myoptions.AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Passive;
app.UseWindowsAzureActiveDirectoryBearerAuthentication(myoptions);
}
}
Run Code Online (Sandbox Code Playgroud)
我添加了一个带有动作的控制器,我希望可以使用持有者令牌访问该动作.
这是控制器:
public class TestController : Controller
{
[Authorize]
public JsonResult Index()
{
return Json(3, JsonRequestBehavior.AllowGet);
} …Run Code Online (Sandbox Code Playgroud)