本文整理汇总了C#中IHttpRequest.TryResolve方法的典型用法代码示例。如果您正苦于以下问题:C# IHttpRequest.TryResolve方法的具体用法?C# IHttpRequest.TryResolve怎么用?C# IHttpRequest.TryResolve使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IHttpRequest
的用法示例。
在下文中一共展示了IHttpRequest.TryResolve方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AuthenticateIfBasicAuth
public static void AuthenticateIfBasicAuth(IHttpRequest req, IHttpResponse res)
{
var userPass = req.GetBasicAuthUserAndPassword();
if (userPass != null)
{
var authService = req.TryResolve<AuthService>();
authService.RequestContext = new HttpRequestContext(req, res, null);
var response = authService.Post(new Auth.Auth {
provider = BasicAuthProvider.Name,
UserName = userPass.Value.Key,
Password = userPass.Value.Value
});
}
}
示例2: ResponseFilter
public void ResponseFilter(IHttpRequest req, IHttpResponse res, object responseDto)
{
// all services should return an IHttpResult (tested convention)
var result = responseDto as IHttpResult;
if (result == null) throwUnconventionalResult(responseDto);
else
{
if (result.StatusCode == HttpStatusCode.NotFound)
{
var log = req.TryResolve<ILogFactory>();
var ex = new ObjectNotFoundException(((IHttpError) result).ErrorCode);
log.GetLogger(string.Empty).Warn("No results returned", ex);
}
}
}
示例3: AuthenticateIfBasicAuth
//Also shared by RequiredRoleAttribute and RequiredPermissionAttribute
public static void AuthenticateIfBasicAuth(IHttpRequest req, IHttpResponse 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<AuthService>();
authService.RequestContext = new HttpRequestContext(req, res, null);
var response = authService.Post(new Auth.Auth {
provider = BasicAuthProvider.Name,
UserName = userPass.Value.Key,
Password = userPass.Value.Value
});
}
}
示例4: Execute
public override void Execute(IHttpRequest req, IHttpResponse res, object requestDto)
{
if (AuthService.AuthConfigs == null) throw new InvalidOperationException("The AuthService must be initialized by calling "
+ "AuthService.Init to use an authenticate attribute");
var matchingOAuthConfigs = AuthService.AuthConfigs.Where(x =>
this.Provider.IsNullOrEmpty()
|| x.Provider == this.Provider).ToList();
if (matchingOAuthConfigs.Count == 0)
{
res.WriteError(req, requestDto, "No OAuth Configs found matching {0} provider"
.Fmt(this.Provider ?? "any"));
res.Close();
return;
}
var userPass = req.GetBasicAuthUserAndPassword();
if (userPass != null)
{
var authService = req.TryResolve<AuthService>();
authService.RequestContext = new HttpRequestContext(req, res, requestDto);
var response = authService.Post(new Auth.Auth {
provider = BasicAuthConfig.Name,
UserName = userPass.Value.Key,
Password = userPass.Value.Value
});
}
using (var cache = req.GetCacheClient())
{
var sessionId = req.GetPermanentSessionId();
var session = sessionId != null ? cache.GetSession(sessionId) : null;
if (session == null || !matchingOAuthConfigs.Any(x => session.IsAuthorized(x.Provider)))
{
res.StatusCode = (int)HttpStatusCode.Unauthorized;
res.AddHeader(HttpHeaders.WwwAuthenticate, "{0} realm=\"{1}\""
.Fmt(matchingOAuthConfigs[0].Provider, matchingOAuthConfigs[0].AuthRealm));
res.Close();
}
}
}
示例5: HasAllRoles
public bool HasAllRoles(IHttpRequest req, IAuthSession session, IUserAuthRepository userAuthRepo=null)
{
if (HasAllRoles(session)) return true;
if (userAuthRepo == null)
userAuthRepo = req.TryResolve<IUserAuthRepository>();
if (userAuthRepo == null) return false;
var userAuth = userAuthRepo.GetUserAuth(session, null);
session.UpdateSession(userAuth);
if (HasAllRoles(session))
{
req.SaveSession(session);
return true;
}
return false;
}
示例6: Execute
public override void Execute(IHttpRequest req, IHttpResponse res, object requestDto)
{
base.Execute(req, res, requestDto);
var session = req.GetSession();
if(session!=null && session.IsAuthenticated)
{
req.SaveSession(session);// refresh session TTL
var cache = req.TryResolve<IRedisClientsManager>();
if(cache!=null){
using(var client = cache.GetClient()){
var pattern = string.Format("urn:{0}:*", req.GetSessionId());
var keys =client.SearchKeys(pattern);
foreach(var k in keys) {
client.ExpireEntryIn(k, AuthProvider.DefaultSessionExpiry);
}
}
}
}
}
示例7: AuthenticateIfDigestAuth
public static void AuthenticateIfDigestAuth(IHttpRequest req, IHttpResponse 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<AuthService>();
authService.RequestContext = new HttpRequestContext(req, res, null);
var response = authService.Post(new Auth.Auth
{
provider = DigestAuthProvider.Name,
nonce = digestAuth["nonce"],
uri = digestAuth["uri"],
response = digestAuth["response"],
qop = digestAuth["qop"],
nc = digestAuth["nc"],
cnonce = digestAuth["cnonce"],
UserName = digestAuth["username"]
});
}
}
示例8: Post
public static Response<User> Post(this User request,
Factory factory,
IHttpRequest httpRequest)
{
if(request.IsDummyPassword())
throw HttpError.Unauthorized("password no válido");
var authRepo = httpRequest.TryResolve<IUserAuthRepository>();
if(authRepo==null)
throw HttpError.NotFound("AuthRepository NO configurado");
var user= new UserAuth
{
FirstName= request.FirstName,
LastName= request.LastName,
Email= request.Email,
UserName= request.UserName,
DisplayName = request.FirstName +" "+ request.LastName
};
user.Set<UserMeta>( new UserMeta{
Cargo= request.Cargo,
Activo=request.Activo,
ExpiresAt= request.ExpiresAt
});
user = authRepo.CreateUserAuth(user, request.Password);
request.Id= user.Id;
request.SetDummyPassword();
List<User> data = new List<User>();
data.Add(request);
return new Response<User>(){
Data=data
};
}
示例9: Put
public static Response<User> Put(this User request,
Factory factory,
IHttpRequest httpRequest)
{
var userSession = httpRequest.GetSession();
if(!( userSession.HasRole(RoleNames.Admin) ||
userSession.HasPermission("User.update") ))
throw HttpError.Unauthorized("Usuario no autorizado para actualizar");
var authRepo = httpRequest.TryResolve<IUserAuthRepository>();
if(authRepo==null)
throw HttpError.NotFound("AuthRepository NO configurado");
var user= authRepo.GetUserAuth(request.Id.ToString());
if (!(request.Id== int.Parse(userSession.UserAuthId) ||
userSession.HasRole(RoleNames.Admin)) )
throw HttpError.Unauthorized("No puede cambiar los datos de otro usuario");
if(user == default(UserAuth))
throw HttpError.NotFound(
string.Format("Usuario con Id:'{0}' NO encontrado",request.Id));
var newUser= new UserAuth
{
Id= request.Id,
FirstName= request.FirstName,
LastName= request.LastName,
Email= request.Email,
UserName= request.UserName,
DisplayName= request.FirstName+" "+request.LastName,
ModifiedDate= System.DateTime.Now,
};
newUser.Set<UserMeta>( new UserMeta{
Cargo= request.Cargo,
Activo=request.Activo,
ExpiresAt= request.ExpiresAt
});
if(request.Password.IsNullOrEmpty()
|| request.IsDummyPassword()){
factory.Execute(proxy=>{
proxy.Update<UserAuth>(
newUser,
ev=>ev.Where(q=>q.Id==request.Id).
Update(f=> new {
f.UserName, f.FirstName, f.LastName, f.Email, f.Meta,
f.DisplayName,
f.ModifiedDate
}));
});
}
else
user = authRepo.UpdateUserAuth(user, newUser,request.Password);
request.SetDummyPassword();
List<User> data = new List<User>();
data.Add(request);
return new Response<User>(){
Data=data
};
}
示例10: UpdateFromUserAuthRepo
public static void UpdateFromUserAuthRepo(this IAuthSession session, IHttpRequest req, IUserAuthRepository userAuthRepo = null)
{
if (userAuthRepo == null)
userAuthRepo = req.TryResolve<IUserAuthRepository>();
if (userAuthRepo == null) return;
var userAuth = userAuthRepo.GetUserAuth(session, null);
session.UpdateSession(userAuth);
}