本文整理匯總了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());
}