C# 如何在ASMX(web参考)中实现OAuth 2.0?
我对实现OAuth2.0非常陌生。我已经在我的xamarin项目中实现了web引用(ASMX文件)。现在我需要实现OAuth2.0进行身份验证。但是我不确定ASMX是否支持OAuth2.0。我是否需要实现WCF而不是Web引用(ASMX),或者是否可以在Web引用(ASMX)中实现OAuth2.0?一个可能的解决方案是将从授权机构获得的令牌添加到授权标头。 您的代码可能与此类似:C# 如何在ASMX(web参考)中实现OAuth 2.0?,c#,wcf,xamarin,oauth-2.0,asmx,C#,Wcf,Xamarin,Oauth 2.0,Asmx,我对实现OAuth2.0非常陌生。我已经在我的xamarin项目中实现了web引用(ASMX文件)。现在我需要实现OAuth2.0进行身份验证。但是我不确定ASMX是否支持OAuth2.0。我是否需要实现WCF而不是Web引用(ASMX),或者是否可以在Web引用(ASMX)中实现OAuth2.0?一个可能的解决方案是将从授权机构获得的令牌添加到授权标头。 您的代码可能与此类似: var redirectURI = Windows.Security.Authentication.Web.WebA
var redirectURI = Windows.Security.Authentication.Web.WebAuthenticationBroker.GetCurrentApplicationCallbackUri();
var _authContext = new AuthenticationContext(authority);
var tokenResult = await _authContext.AcquireTokenAsync(serviceResourceId, clientId, redirectURI);
if (tokenResult.Status != AuthenticationStatus.Success)
{
//Not authenticated
return;
}
var svc = new YourServiceReference.YourClient();
using (var scope = new OperationContextScope(svc.InnerChannel))
{
var httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = tokenResult.AccessToken;
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
var result = svc.MyFunction();
//Do something with the data
}
棘手-这是移动OAuth解决方案的工作方式-但这是一项艰巨的工作:
- 您的Xamarin应用程序需要实现移动SSO才能让用户登录
- Xamarin应用程序的用户将被重定向到通过授权服务器/身份提供商登录,之后他们将收到OAuth访问令牌
- 登录后,您的Xamarin应用程序将能够使用令牌调用API,并代表用户获取数据
- API需要验证收到的访问令牌