本文整理汇总了C#中IHttpRequest.GetBasicAuthUserAndPassword方法的典型用法代码示例。如果您正苦于以下问题:C# IHttpRequest.GetBasicAuthUserAndPassword方法的具体用法?C# IHttpRequest.GetBasicAuthUserAndPassword怎么用?C# IHttpRequest.GetBasicAuthUserAndPassword使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IHttpRequest
的用法示例。
在下文中一共展示了IHttpRequest.GetBasicAuthUserAndPassword方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AuthenticateBasicAuth
//Also shared by RequiredRoleAttribute and RequiredPermissionAttribute
public static User AuthenticateBasicAuth(IHttpRequest req, IHttpResponse res)
{
var userCredentialsPair = req.GetBasicAuthUserAndPassword();
var email = userCredentialsPair.HasValue ? userCredentialsPair.Value.Key : String.Empty;
var password = userCredentialsPair.HasValue ? userCredentialsPair.Value.Value : String.Empty;
User user = null;
bool isValid = false;
using (var session = NHibernateHelper.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var userQuery = session.QueryOver<User>()
.Where(table => table.Email == email)
.And(table => table.Password == password);
user = userQuery.SingleOrDefault();
transaction.Commit();
isValid = (user != null);
}
}
if (!isValid)
{
res.StatusCode = (int)HttpStatusCode.Unauthorized;
res.EndServiceStackRequest();
}
return user;
}
示例2: Authorize
public void Authorize(IHttpRequest httpRequest, IHttpResponse httpResponse, object arg3)
{
var apikey = httpRequest.GetBasicAuthUserAndPassword();
bool authorized = IsAuthorized(apikey);
if (!authorized)
{
httpResponse.ReturnAuthRequired();
return;
}
// Don't need to do anything if a valid API key
}
示例3: 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
});
}
}
示例4: 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
});
}
}
示例5: 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();
}
}
}
示例6: getUser
public static ApiUser getUser(IHttpRequest request)
{
var basicAuth = request.GetBasicAuthUserAndPassword();
string key = basicAuth.Value.Key;
string api_token = basicAuth.Value.Value;
if (key.Length != 13 || !key[6].Equals('-'))
throw new HttpError(HttpStatusCode.Forbidden, "Org/Instance is not correct.");
string[] split = key.Split('-');
string org_key = split[0];
string instance_key = split[1];
if (api_token.Length != 32)
throw new HttpError(HttpStatusCode.Forbidden, "Token is not correct.");
ApiUser apiUser = new ApiUser(api_token);
if (!apiUser.ValidateAccess(org_key, instance_key))
throw new HttpError(HttpStatusCode.Forbidden, "User is Inactive or does not have access to this Organization/Instance");
return apiUser;
}
示例7: Execute
public override void Execute(IHttpRequest httpReq, IHttpResponse httpResp, object request)
{
var basicAuth = httpReq.GetBasicAuthUserAndPassword();
ApiRequest apiRequest = request as ApiRequest;
if (apiRequest == null)
{
//Custom Auth needed
return;
}
string api_token = "";
if (basicAuth == null)
{
api_token = httpReq.QueryString["api_token"];
if (string.IsNullOrEmpty(api_token))
{
httpResp.AddHeader(HttpHeaders.WwwAuthenticate, "Basic realm=\"/login\"");
throw new HttpError(HttpStatusCode.Unauthorized, "Invalid BasicAuth credentials");
}
else if (api_token.Length != 32)
throw new HttpError(HttpStatusCode.Forbidden, "Token is not correct.");
}
else
{
string key = basicAuth.Value.Key;
string password = basicAuth.Value.Value;
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(password))
throw new HttpError(HttpStatusCode.Forbidden, "Token is not correct.");
if (key == "x")
{
if (password.Length != 32)
throw new HttpError(HttpStatusCode.Forbidden, "Token is not correct.");
apiRequest.api_token = password;
}
else
{
apiRequest.ApiUser = ApiUser.getUser(httpReq);
}
}
}