gpt4 book ai didi

.net - 将 API key /身份验证信息从 HttpOperationHandler 传递到 WCF4 webservice 中的 API 类

转载 作者:行者123 更新时间:2023-12-02 00:26:26 25 4
gpt4 key购买 nike

我目前正在开发基于 WCF4 的 REST 网络服务。大多数调用不需要授权,但有些调用需要授权,因此我的计划是实现一个 RequireAuthorization 属性来处理某些调用的 API key 授权。

看完Implementing an Authorization Attribute for WCF Web API我在 OnHandle 方法中实现了一个处理 API key 验证的 HttpOperationHandler,并将其添加到服务配置中的 RequestHandlers。

基本上这工作正常,但我需要将我在 AuthOperationHandler 中提取的用户信息传递给服务,但我找不到任何相关信息。

有人能帮忙吗?

public class ServiceConfiguration : WebApiConfiguration
{
public ServiceConfiguration()
{
RequestHandlers = (c, e, od) => {
var authorizeAttribute = od.Attributes.OfType<RequireAuthorizationAttribute>().FirstOrDefault();
if (authorizeAttribute != null)
{
c.Add(new AuthOperationHandler(authorizeAttribute));
}
};

CreateInstance = (serviceType, context, request) => RepositoryFactory.GetInstance(serviceType);
}
}

public class AuthOperationHandler : HttpOperationHandler<HttpRequestMessage, HttpRequestMessage>
{
protected override HttpRequestMessage OnHandle(HttpRequestMessage input)
{
// API Key validation here
var user = RetrieveUserByApiKey(input);

return input;
}
}

最佳答案

我找到了一个解决方案,但它确实有一个警告。

我的做法是让我的操作处理程序返回某种形式的自定义身份验证状态类,而不是 HttpResponsemessage - 在我的例子中,我使用了 AuthenticationTicket。

public class AuthOperationHandler : HttpOperationHandler<HttpRequestMessage, AuthenticationTicket>
{
protected override AuthenticationTicket OnHandle(HttpRequestMessage input)
{
var ticket = RetrieveAuthenticationTicket(input);

return ticket;
}
}

AuthenticationTicket 类只存储帐户 ID。

    public class AuthenticationTicket
{
public AuthenticationTicket(int accountId)
{
AccountId = accountId;
}

public int AccountId { get; set; }
}

然后在您的服务方法中添加一个 AuthenticationTicket 作为参数,这将由操作处理程序为您填充。

    [OperationContract]
[WebInvoke(UriTemplate = "people", Method = "GET")]
HttpResponseMessage<IList<Person>> LoadPeople(AuthenticationTicket ticket);

然后您可以在您的方法实现中使用票证。

但是,如果您需要使用额外的参数,这对于 POST、PUT 和 DELETE 操作来说有点滑稽,如下所示:

    [OperationContract]
[WebInvoke(UriTemplate = "people", Method = "PUT")]
HttpResponseMessage AddPerson(AuthenticationTicket ticket, Person person);

您将获得以下几行的异常:

The HttpOperationHandlerFactory is unable to determine the input parameter that should be associated with the request message content for service operation 'AddPerson'. If the operation does not expect content in the request message use the HTTP GET method with the operation. Otherwise, ensure that one input parameter either has it's IsContentParameter property set to 'True' or is a type that is assignable to one of the following: HttpContent, ObjectContent1,
HttpRequestMessage or HttpRequestMessage
1.

我找到的解决方案(不确定是否正确)是将资源包装在 HttpRequestMessage 中,如下所示:

    [OperationContract]
[WebInvoke(UriTemplate = "people", Method = "PUT")]
HttpResponseMessage UpdatePerson(AuthenticationTicket ticket, HttpRequestMessage<Person> request);

然后在您的方法中使用以下方法检索 Person:

var person = request.Content.ReadAsAsync<Person>().Result;

希望这对我有帮助,它对我有用。但我仍在学习如何诚实,所以可能有更好的方法。

关于.net - 将 API key /身份验证信息从 HttpOperationHandler 传递到 WCF4 webservice 中的 API 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8817478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com