本文整理汇总了C#中IRequest.TryResolve方法的典型用法代码示例。如果您正苦于以下问题:C# IRequest.TryResolve方法的具体用法?C# IRequest.TryResolve怎么用?C# IRequest.TryResolve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IRequest
的用法示例。
在下文中一共展示了IRequest.TryResolve方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Execute
public override void Execute(IRequest req, IResponse res, object requestDto)
{
if (req.IsLocal)
return;
var appConfig = req.TryResolve<AppConfig>();
var apiKey = req.Headers["x-api-key"] ?? req.QueryString["api_key"];
if (apiKey == null || !appConfig.ReadWriteApiKeys.Contains(apiKey))
{
throw HttpError.Unauthorized("Unauthorized. Valid x-api-key header required.");
}
}
示例2: PreAuthenticate
public void PreAuthenticate(IRequest req, IResponse res)
{
//Need to run SessionFeature filter since its not executed before this attribute (Priority -100)
SessionFeature.AddSessionIdToRequestFilter(req, res, null); //Required to get req.GetSessionId()
var userPass = req.GetBasicAuthUserAndPassword();
if (userPass != null)
{
var authService = req.TryResolve<AuthenticateService>();
authService.Request = req;
var response = authService.Post(new Authenticate
{
provider = Name,
UserName = userPass.Value.Key,
Password = userPass.Value.Value
});
}
}
示例3: HasAllPermissions
public bool HasAllPermissions(IRequest req, IAuthSession session, IAuthRepository userAuthRepo=null)
{
if (HasAllPermissions(session)) return true;
if (userAuthRepo == null)
userAuthRepo = req.TryResolve<IAuthRepository>();
if (userAuthRepo == null) return false;
var userAuth = userAuthRepo.GetUserAuth(session, null);
session.UpdateSession(userAuth);
if (HasAllPermissions(session))
{
req.SaveSession(session);
return true;
}
return false;
}
示例4: AssertRequiredRoles
/// <summary>
/// Check all session is in any supplied roles otherwise a 401 HttpError is thrown
/// </summary>
/// <param name="request"></param>
/// <param name="requiredRoles"></param>
public static void AssertRequiredRoles(IRequest request, params string[] requiredRoles)
{
if (requiredRoles.IsEmpty()) return;
var req = request.TryResolve<IHttpRequest>();
if (HostContext.HasValidAuthSecret(req))
return;
var session = req.GetSession();
if (session != null && session.UserAuthId != null && requiredRoles.Any(session.HasRole))
return;
session.UpdateFromUserAuthRepo(req);
if (session != null && session.UserAuthId != null && requiredRoles.Any(session.HasRole))
return;
var statusCode = session != null && session.IsAuthenticated
? (int)HttpStatusCode.Forbidden
: (int)HttpStatusCode.Unauthorized;
throw new HttpError(statusCode, "Invalid Role");
}
示例5: PreAuthenticate
public void PreAuthenticate(IRequest req, IResponse res)
{
//Need to run SessionFeature filter since its not executed before this attribute (Priority -100)
SessionFeature.AddSessionIdToRequestFilter(req, res, null); //Required to get req.GetSessionId()
var digestAuth = req.GetDigestAuth();
if (digestAuth != null)
{
var authService = req.TryResolve<AuthenticateService>();
authService.Request = req;
var response = authService.Post(new Authenticate
{
provider = Name,
nonce = digestAuth["nonce"],
uri = digestAuth["uri"],
response = digestAuth["response"],
qop = digestAuth["qop"],
nc = digestAuth["nc"],
cnonce = digestAuth["cnonce"],
UserName = digestAuth["username"]
});
}
}
示例6: PreAuthenticate
public void PreAuthenticate(IRequest request, IResponse response)
{
var header = request.Headers["Authorization"];
var authService = request.TryResolve<AuthenticateService>();
authService.Request = request;
// pass auth header in as oauth token to authentication
authService.Post(new Authenticate
{
provider = Name,
oauth_token = header
});
}
示例7: GetPageName
public string GetPageName(object dto, IRequest requestContext)
{
var httpRequest = requestContext != null ? requestContext.TryResolve<IHttpRequest>() : null;
var httpResult = dto as IHttpResult;
if (httpResult != null)
{
dto = httpResult.Response;
}
if (dto != null) return dto.GetType().Name;
return httpRequest != null ? httpRequest.OperationName : null;
}
示例8: PreAuthenticate
public void PreAuthenticate(IRequest req, IResponse res)
{
SessionFeature.AddSessionIdToRequestFilter(req, res, null); //Required to get req.GetSessionId()
var authService = req.TryResolve<AuthenticateService>();
authService.Request = req;
var user = req.GetUser();
if (user != null)
{
var session = req.GetSession();
if (LoginMatchesSession(session, user.Identity.Name)) return;
var response = authService.Post(new Authenticate
{
provider = Name,
UserName = user.GetUserName(),
});
}
}
示例9: ProcessRequestAsync
public override Task ProcessRequestAsync(IRequest req, IResponse res, string operationName)
{
res.ContentType = MimeTypes.ServerSentEvents;
res.AddHeader(HttpHeaders.CacheControl, "no-cache");
res.KeepAlive = true;
res.Flush();
IAuthSession session = req.GetSession();
var userAuthId = session != null ? session.UserAuthId : null;
var feature = HostContext.GetPlugin<SimpleServerEventsFeature>();
//var now = DateTime.UtcNow;
var subscriptionId = SessionExtensions.CreateRandomSessionId();
var eventClient = new EventClient(res)
{
SubscriptionId = subscriptionId, // A session can have multiple subscriptions / clients
};
if (feature.OnCreated != null)
feature.OnCreated(eventClient, req);
//var heartbeatUrl = req.ResolveAbsoluteUrl("~/".CombineWith(feature.HeartbeatPath))
// .AddQueryParam("id", subscriptionId);
var privateArgs = new Dictionary<string, string>(eventClient.Meta) {
{"id", subscriptionId },
//{"heartbeatUrl", heartbeatUrl},
//{"heartbeatIntervalMs", ((long)feature.HeartbeatInterval.TotalMilliseconds).ToString(CultureInfo.InvariantCulture) }
};
// Register the client
req.TryResolve<IServerEventsBroker>().Connect(eventClient);
if (feature.OnConnected != null)
feature.OnConnected(eventClient, privateArgs);
// Welcome our new client
res.OutputStream.Write(": " + subscriptionId + " connected\n\n"); // Write initial stream so the client knows we're alive
res.Flush();
var tcs = new TaskCompletionSource<bool>();
eventClient.OnDispose = client =>
{
try
{
res.EndHttpHandlerRequest(skipHeaders: true);
}
catch { }
tcs.SetResult(true);
};
return tcs.Task;
}
示例10: PreAuthenticate
public void PreAuthenticate(IRequest req, IResponse res)
{
//Need to run SessionFeature filter since its not executed before this attribute (Priority -100)
SessionFeature.AddSessionIdToRequestFilter(req, res, null); //Required to get req.GetSessionId()
var httpRequestBase = req.ToHttpRequestBase();
var bearerToken = httpRequestBase.QueryString["access_token"] ?? "bearer-token-value";
//var unityStsProxyService = new UnityStsProxyService();// req.TryResolve<UnityStsProxyService>();
var unityStsProxyService = req.TryResolve<UnityStsProxyService>();
unityStsProxyService.Request = req;
req.SetSessionId(bearerToken);
var session = req.GetSession();
if (!session.IsAuthenticated)
{
unityStsProxyService.Any(new GetTokenInfo { BearerToken = bearerToken });
}
}
示例11: OnRegistered
public override void OnRegistered(IRequest httpReq, IAuthSession session, IServiceBase registrationService)
{
var apiKeys = apiKeyProvider.GenerateNewApiKeys(session.UserAuthId);
var authRepo = (IManageApiKeys)httpReq.TryResolve<IAuthRepository>().AsUserAuthRepository(httpReq);
authRepo.StoreAll(apiKeys);
}
示例12: PreAuthenticateWithApiKey
private static void PreAuthenticateWithApiKey(IRequest req, IResponse res, string apiKey)
{
//Need to run SessionFeature filter since its not executed before this attribute (Priority -100)
SessionFeature.AddSessionIdToRequestFilter(req, res, null); //Required to get req.GetSessionId()
using (var authService = req.TryResolve<AuthenticateService>())
{
authService.Request = req;
var response = authService.Post(new Authenticate
{
provider = Name,
UserName = "ApiKey",
Password = apiKey,
});
}
}
示例13: PreAuthenticate
public void PreAuthenticate(IRequest req, IResponse res)
{
//The API Key is sent in the Basic Auth Username and Password is Empty
var userPass = req.GetBasicAuthUserAndPassword();
if (userPass != null && string.IsNullOrEmpty(userPass.Value.Value))
{
if (RequireSecureConnection && !req.IsSecureConnection)
throw HttpError.Forbidden(ErrorMessages.ApiKeyRequiresSecureConnection);
var apiKey = userPass.Value.Key;
PreAuthenticateWithApiKey(req, res, apiKey);
}
var bearerToken = req.GetBearerToken();
if (bearerToken != null)
{
var authRepo = (IManageApiKeys)req.TryResolve<IAuthRepository>().AsUserAuthRepository(req);
if (authRepo.ApiKeyExists(bearerToken))
{
if (RequireSecureConnection && !req.IsSecureConnection)
throw HttpError.Forbidden(ErrorMessages.ApiKeyRequiresSecureConnection);
PreAuthenticateWithApiKey(req, res, bearerToken);
}
}
}
示例14: OnRegistered
public override void OnRegistered(IRequest httpReq, IAuthSession session, IServiceBase registrationService)
{
var now = DateTime.UtcNow;
var userId = session.UserAuthId;
var apiKeys = new List<ApiKey>();
foreach (var env in apiKeyProvider.Environments)
{
foreach (var keyType in apiKeyProvider.KeyTypes)
{
var key = apiKeyProvider.CreateApiKeyFn(env, keyType, apiKeyProvider.KeySizeBytes);
var apiKey = new ApiKey
{
UserAuthId = userId,
Environment = env,
KeyType = keyType,
Id = key,
CreatedDate = now,
};
if (apiKeyProvider.ApiKeyFilterFn != null)
apiKeyProvider.ApiKeyFilterFn(apiKey);
apiKeys.Add(apiKey);
}
}
var authRepo = (IManageApiKeys)httpReq.TryResolve<IAuthRepository>().AsUserAuthRepository(httpReq);
authRepo.StoreAll(apiKeys);
}