Ada*_*itt 3 rest dotnetopenauth
我在ASP.NET MVC 4 Web应用程序的Area中创建了一个REST API.API工作正常,我现在想要保护它.
有一个非常简单的例子说明我如何做到这一点?我正在浏览DotNetOpenAuth下载附带的示例,我完全迷失了它.
几天前我遇到了同样的问题.这个答案非常漫长,也许还有一个更简单的方法.
就个人而言,我不再使用DNOA,因为它是为自我验证(即加密的令牌)而设计的,因此您无需在每次请求时都使用DB.这样做的一个非常重要的副作用是访问撤销不会立即生效,而是仅在必须续订令牌之后才生效.此外,访问令牌将变得非常长(大约500字节).
作为第一步,确保您知道您需要什么:
OAuth/OAuth2起初看起来很简单,但了解授权工作流程的设计方式非常重要.此外,他们的术语可能会令人恼火,例如"客户"指的是我天真地称之为客户端应用程序.这不是用户(在OAuth术语中称为"资源所有者").我的建议:阅读RFC 6749.它看起来很沉闷,但这是一个有趣的阅读(你可以跳过一半......)
一个关键问题是:您需要两条腿OAuth还是三条腿OAuth(或两者兼而有之?).您需要支持哪些授权类型?
如果您基本上想要替换HTTP Basic Auth,那么简单的"资源所有者密码凭证流"就可以了.facebook/twitter类型的"让这个应用程序访问我的个人资料信息"是三条腿OAuth.
有一个IBM文档,附带了很好的授权类型图.
现在到DNOA,看看吧Samples/OAuthAuthorizationServer.
一个很好的切入点是OAuthController.cs文件.请注意,仅当您希望允许用户授予对第三方应用程序(三方OAuth)的访问权限时,才需要执行Authorize和AuthorizeResponse操作.
在两条腿的情况下,用户token直接访问OAuth 端点并简单地请求访问令牌.无论如何,您的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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
资源服务器仍然缺失
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);
}
}
Run Code Online (Sandbox Code Playgroud)
接下来,修改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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2053 次 |
| 最近记录: |