本文整理汇总了C#中System.Net.Http.HttpRequestMessage.SetOwinContext方法的典型用法代码示例。如果您正苦于以下问题:C# HttpRequestMessage.SetOwinContext方法的具体用法?C# HttpRequestMessage.SetOwinContext怎么用?C# HttpRequestMessage.SetOwinContext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Http.HttpRequestMessage
的用法示例。
在下文中一共展示了HttpRequestMessage.SetOwinContext方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendAsync
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpRequestContext context = request.GetRequestContext();
context.Principal = Principal;
request.SetOwinContext(this.OwinContext);
return base.SendAsync(request, cancellationToken);
}
示例2: GetOwinContext_ReturnsSetContextInstance
public void GetOwinContext_ReturnsSetContextInstance()
{
// Arrange
var request = new HttpRequestMessage();
var context = new OwinContext();
request.SetOwinContext(context);
// Act & Asert
Assert.Same(context, request.GetOwinContext());
}
示例3: SetOwinContext_RemovesOwinEnvironmentProperty_WhenPresent
public void SetOwinContext_RemovesOwinEnvironmentProperty_WhenPresent()
{
// Arrange
using (HttpRequestMessage request = new HttpRequestMessage())
{
var environment = new Dictionary<string, object>();
request.Properties.Add("MS_OwinEnvironment", environment);
var context = new OwinContext();
// Act
request.SetOwinContext(context);
// Assert
Assert.False(request.Properties.ContainsKey("MS_OwinEnvironment"));
}
}
示例4: MapRequestProperties
private static void MapRequestProperties(HttpRequestMessage request, IOwinContext context)
{
// Set the OWIN context on the request
request.SetOwinContext(context);
// Set the a request context on the request that lazily populates each property.
HttpRequestContext requestContext = new OwinHttpRequestContext(context, request);
request.SetRequestContext(requestContext);
}
示例5: CreateRequestMessage
private HttpRequestMessage CreateRequestMessage(ClaimsPrincipal user, bool isCompressed, bool isJson, string charset = "utf-8") {
var request = new HttpRequestMessage();
var context = new OwinContext();
context.Request.User = Thread.CurrentPrincipal = user;
request.SetOwinContext(context);
request.SetConfiguration(new HttpConfiguration());
request.Content = new HttpMessageContent(new HttpRequestMessage(HttpMethod.Post, "/api/v2/events"));
if (isCompressed)
request.Content.Headers.ContentEncoding.Add("gzip");
request.Content.Headers.ContentType.MediaType = isJson ? "application/json" : "text/plain";
request.Content.Headers.ContentType.CharSet = charset;
return request;
}
示例6: CreateRequestWithOwinContextAndRequestContext
private static HttpRequestMessage CreateRequestWithOwinContextAndRequestContext(IOwinContext context)
{
HttpRequestMessage request = new HttpRequestMessage();
request.SetOwinContext(context);
request.SetRequestContext(new HttpRequestContext());
return request;
}
示例7: SendAsync_SetsRequestContextPrincipalToAnonymous_BeforeCallingInnerHandler
public void SendAsync_SetsRequestContextPrincipalToAnonymous_BeforeCallingInnerHandler()
{
// Arrange
IPrincipal requestContextPrincipal = null;
Mock<HttpRequestContext> requestContextMock = new Mock<HttpRequestContext>(MockBehavior.Strict);
requestContextMock
.SetupSet(c => c.Principal = It.IsAny<IPrincipal>())
.Callback<IPrincipal>((value) => requestContextPrincipal = value);
IPrincipal principalBeforeInnerHandler = null;
HttpMessageHandler inner = new LambdaHttpMessageHandler((ignore1, ignore2) =>
{
principalBeforeInnerHandler = requestContextPrincipal;
return Task.FromResult<HttpResponseMessage>(null);
});
HttpMessageHandler handler = CreateProductUnderTest(inner);
IOwinContext context = CreateOwinContext();
using (HttpRequestMessage request = new HttpRequestMessage())
{
request.SetOwinContext(context);
request.SetRequestContext(requestContextMock.Object);
// Act
HttpResponseMessage ignore = handler.SendAsync(request, CancellationToken.None).Result;
}
// Assert
Assert.NotNull(principalBeforeInnerHandler);
IIdentity identity = principalBeforeInnerHandler.Identity;
Assert.NotNull(identity);
Assert.False(identity.IsAuthenticated);
Assert.Null(identity.Name);
Assert.Null(identity.AuthenticationType);
}
示例8: MapRequestProperties
private static void MapRequestProperties(HttpRequestMessage request, IOwinContext context)
{
// Set the OWIN context on the request
request.SetOwinContext(context);
// Set the virtual path root for link resolution and link generation to work
// OWIN spec requires request path base to be either the empty string or start with "/"
string requestPathBase = context.Request.PathBase;
request.SetVirtualPathRoot(String.IsNullOrEmpty(requestPathBase) ? "/" : requestPathBase);
// Set a delegate to get the client certificate
request.Properties[HttpPropertyKeys.RetrieveClientCertificateDelegateKey] = new Func<HttpRequestMessage, X509Certificate2>(
req => context.Get<X509Certificate2>(OwinConstants.ClientCertifiateKey));
// Set a lazily-evaluated way of determining whether the request is local or not
Lazy<bool> isLocal = new Lazy<bool>(() => context.Get<bool>(OwinConstants.IsLocalKey), isThreadSafe: false);
request.Properties[HttpPropertyKeys.IsLocalKey] = isLocal;
}
示例9: CreateRequest
private static HttpRequestMessage CreateRequest(IOwinContext owinContext)
{
HttpRequestMessage request = new HttpRequestMessage();
request.SetOwinContext(owinContext);
return request;
}
示例10: SendAsync
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.SetOwinContext(new OwinContext());
return base.SendAsync(request, cancellationToken);
}
示例11: GetOwinEnvironment_ReturnsSetContextEnvironmentInstance
public void GetOwinEnvironment_ReturnsSetContextEnvironmentInstance()
{
// Arrange
var request = new HttpRequestMessage();
var environment = new Dictionary<string, object>();
var context = new OwinContext(environment);
request.SetOwinContext(context);
// Act & Assert
Assert.Same(environment, request.GetOwinEnvironment());
}