DotNetOpenAuth 提供程序用于 REST 服务。

3

我在ASP.NET MVC 4 Web应用程序的区域中创建了一个REST API。该API已经正常运行,现在我想对其进行保护。

有没有非常简单的示例可以让我做到这一点?我正在查看带有DotNetOpenAuth下载的示例,但我完全不知道如何操作。

1个回答

9

几天前我也遇到了同样的问题,这个答案太长了,可能有更简单的方法。

个人而言,我不再使用DNOA,因为它是针对自验证(即加密令牌)设计的,因此您不需要在每个请求中命中数据库。这个非常重要的副作用是,访问吊销将不会立即生效,而是只有在令牌必须更新后才会生效。此外,访问令牌会变得相当长(约500字节)。

作为第一步,确保您知道您需要什么:


OAuth / OAuth2看起来很简单,但重要的是要理解授权工作流程的设计方式。此外,它们的术语可能会引起烦恼,例如“Client”指代我天真地称之为客户端应用程序。它不是用户(在OAuth术语中称为“资源所有者”)。我的建议:阅读RFC 6749。它看起来很乏味,但很有趣(你可以跳过其中一半......)

一个关键问题是:您需要2-legged OAuth还是3-legged OAuth(或两者都需要)?您需要支持哪些授权类型?

如果您基本上想替换HTTP基本身份验证,则简单的“资源所有者密码凭据流”就可以了。 Facebook / Twitter类型的“让此应用程序访问我的配置文件信息”是3-legged OAuth。

有一个带有不错授权类型图的IBM文档。


现在来看DNOA,查看Samples/OAuthAuthorizationServer

OAuthController.cs文件是一个很好的入口点。请注意,只有当您希望使用户授权第三方应用程序(3-legged OAuth)时,才需要AuthorizeAuthorizeResponse操作。

在2-legged情况下,用户直接访问OAuthtoken端点,并简单地请求访问令牌。在任何情况下,您都需要这样的控制器在您的REST应用程序中。

内部工作的关键是OAuth2AuthorizationServer类(而不是AuthorizationServer类)。请查看Code/OAuth2AuthorizationServer.cs。它实现了IAuthorizationServerHost

该类的一半用于数据存储(如果您使用不同的数据存储,则可能需要修改它),另一半用于访问令牌的加密。您也需要为您的应用程序实现IAuthorizationServerHost。

确保您的代码中有#define SAMPLESONLY一行,以便接受硬编码证书。

为了实际授权请求,编写自定义的 ActionFilterAttribute 有助于解决问题。下面是一些超级简洁但不适用于生产环境的代码示例:
public sealed class BasicAuthenticationAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
    private readonly OAuthResourceServer _authServer; 
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (actionContext.Request.Headers.Authorization.Scheme == "Bearer"
            || actionContext.Request.Properties.ContainsKey("access_token"))
        {
            authenticatedUser = _authServer.VerifyOAuth2(request, required_claims);
            HttpContext.Current.User = authenticatedUser;
            Thread.CurrentPrincipal = authenticatedUser;
        }
    }
}

// See OAuthResourceServer/Code/OAuthAuthorizationManager.cs in DNOA samples
public sealed class OAuthResourceServer
{
    public IPrincipal VerifyOAuth2(HttpRequestMessage httpDetails, params string[] requiredScopes)
    {
        // for this sample where the auth server and resource server are the same site,
        // we use the same public/private key.
        using (var signing = CreateAuthorizationServerSigningServiceProvider())
        {
            using (var encrypting = CreateResourceServerEncryptionServiceProvider())
            {
                var tokenAnalyzer = new StandardAccessTokenAnalyzer(signing, encrypting);
                var resourceServer = new ResourceServer(_myUserService, tokenAnalyzer);
                return resourceServer.GetPrincipal(httpDetails, requiredScopes);
            }
        }
    }
}

资源服务器仍然缺失。
public sealed class MyResourceServer : ResourceServer
{
    public override System.Security.Principal.IPrincipal GetPrincipal([System.Runtime.InteropServices.OptionalAttribute]
        [System.Runtime.InteropServices.DefaultParameterValueAttribute(null)]
        HttpRequestBase httpRequestInfo, params string[] requiredScopes)
    {
        AccessToken accessToken = this.GetAccessToken(httpRequestInfo, requiredScopes);
        string principalUserName = !string.IsNullOrEmpty(accessToken.User)
            ? this.ResourceOwnerPrincipalPrefix + accessToken.User
            : this.ClientPrincipalPrefix + accessToken.ClientIdentifier;
        string[] principalScope = accessToken.Scope != null ? accessToken.Scope.ToArray() : new string[0];

        // Now your own code that retrieves the user 
        // based on principalUserName from the DB:
        return myUserService.GetUser(userName);
    }
}

接下来,修改web.config文件,以便在开发过程中DNOA不会抱怨缺少SSL连接:
<configSections>
      <sectionGroup name="dotNetOpenAuth" type="DotNetOpenAuth.Configuration.DotNetOpenAuthSection, DotNetOpenAuth">
        <section name="openid" type="DotNetOpenAuth.Configuration.OpenIdElement, DotNetOpenAuth" requirePermission="false" allowLocation="true" />
        <section name="oauth" type="DotNetOpenAuth.Configuration.OAuthElement, DotNetOpenAuth" requirePermission="false" allowLocation="true" />
        <sectionGroup name="oauth2" type="DotNetOpenAuth.Configuration.OAuth2SectionGroup, DotNetOpenAuth">
          <section name="authorizationServer" type="DotNetOpenAuth.Configuration.OAuth2AuthorizationServerSection, DotNetOpenAuth" requirePermission="false" allowLocation="true" />
        </sectionGroup>
        <section name="messaging" type="DotNetOpenAuth.Configuration.MessagingElement, DotNetOpenAuth" requirePermission="false" allowLocation="true" />
        <section name="reporting" type="DotNetOpenAuth.Configuration.ReportingElement, DotNetOpenAuth" requirePermission="false" allowLocation="true" />
      </sectionGroup>    
  </configSections>
  <dotNetOpenAuth>
    <!-- Allow DotNetOpenAuth to publish usage statistics to library authors to improve the library. -->
    <reporting enabled="true" />
    <openid>
      <provider>
        <security requireSsl="false">
        </security>
      </provider>
    </openid>
    <oauth2>
      <authorizationServer >
      </authorizationServer>
    </oauth2>
    <!-- Relaxing SSL requirements is useful for simple samples, but NOT a good idea in production. -->
    <messaging relaxSslRequirements="true">
      <untrustedWebRequest>
        <whitelistHosts>
          <!-- since this is a sample, and will often be used with localhost -->
          <add name="localhost"/>
        </whitelistHosts>
      </untrustedWebRequest>
    </messaging>
  </dotNetOpenAuth>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接