Asp.net 如何在Web API中使用Google风格的令牌身份验证

Asp.net 如何在Web API中使用Google风格的令牌身份验证,asp.net,asp.net-mvc,authentication,asp.net-web-api,identity,Asp.net,Asp.net Mvc,Authentication,Asp.net Web Api,Identity,我有一个内部使用的网站,将在互联网上公开,以便于在移动设备上使用。该网站是MVC5,将与不同服务器上的web API进行通信。用户将在登录页面上输入他们的Windows帐户信息,登录页面将根据我们的Active Directory服务进行身份验证。一旦通过身份验证,我想创建一个身份验证令牌,用于对MVC站点的后续调用以及对各种其他Web API的调用 起初,我们只打算使用基本身份验证,因为所有通信通道都通过SSL,但是我们有一个Web API,它不能访问我们的AD,但可以访问可能包含令牌信息的中

我有一个内部使用的网站,将在互联网上公开,以便于在移动设备上使用。该网站是MVC5,将与不同服务器上的web API进行通信。用户将在登录页面上输入他们的Windows帐户信息,登录页面将根据我们的Active Directory服务进行身份验证。一旦通过身份验证,我想创建一个身份验证令牌,用于对MVC站点的后续调用以及对各种其他Web API的调用

起初,我们只打算使用基本身份验证,因为所有通信通道都通过SSL,但是我们有一个Web API,它不能访问我们的AD,但可以访问可能包含令牌信息的中央数据库


任何关于如何保护企业Web API的示例或文档都将非常棒。我找不到有关此主题的详细信息。

一种方法是创建自定义ActionFilterAttribute并重写OnActionExecuting方法


然后,您可以将此属性应用于api中要验证的任何操作或方法。

我们最终使用了thinktecture的IdentityServer。

v3测试版1刚刚发布。看起来很有希望。 支持OpenID Connect和OAuth2.0

他们有几个客户机示例,您可以从不同的存储库下载。

public class AuthenticateAttribute : ActionFilterAttribute
{
     public override void OnActionExecuting(HttpActionContext actionContext)
     {
          //grab token from httprequest in the actionContext
          //and authenticate it against the token in the database

         if(/*token is NOT authenticated*/)
         {
              //set unauthorised response
              var response = actionContext.ControllerContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
             //and set the httpresponse in the actionContext to the unauthorised response
             actionContext.Response = response;
             //then return
             return;
         }
     }
}