本文整理汇总了C#中IHttpContext.SetStatusToUnauthorized方法的典型用法代码示例。如果您正苦于以下问题:C# IHttpContext.SetStatusToUnauthorized方法的具体用法?C# IHttpContext.SetStatusToUnauthorized怎么用?C# IHttpContext.SetStatusToUnauthorized使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IHttpContext
的用法示例。
在下文中一共展示了IHttpContext.SetStatusToUnauthorized方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Authorize
public override bool Authorize(IHttpContext ctx)
{
if (server.DefaultConfiguration.AnonymousUserAccessMode == AnonymousUserAccessMode.None && IsInvalidUser(ctx))
{
var requestUrl = ctx.GetRequestUrl();
if (neverSecretUrls.Contains(requestUrl, StringComparer.InvariantCultureIgnoreCase))
return true;
ctx.SetStatusToUnauthorized();
return false;
}
var httpRequest = ctx.Request;
if (server.DefaultConfiguration.AnonymousUserAccessMode == AnonymousUserAccessMode.Get &&
IsInvalidUser(ctx) &&
IsGetRequest(httpRequest.HttpMethod, httpRequest.Url.AbsolutePath) == false)
{
var requestUrl = ctx.GetRequestUrl();
if (neverSecretUrls.Contains(requestUrl, StringComparer.InvariantCultureIgnoreCase))
return true;
ctx.SetStatusToUnauthorized();
return false;
}
return true;
}
示例2: Authorize
public override bool Authorize(IHttpContext ctx)
{
if (server.DefaultConfiguration.AnonymousUserAccessMode == AnonymousUserAccessMode.None && IsInvalidUser(ctx))
{
var requestUrl = ctx.GetRequestUrl();
if (NeverSecret.Urls.Contains(requestUrl, StringComparer.InvariantCultureIgnoreCase))
return true;
ctx.SetStatusToUnauthorized();
return false;
}
var httpRequest = ctx.Request;
if (server.DefaultConfiguration.AnonymousUserAccessMode == AnonymousUserAccessMode.Get &&
IsInvalidUser(ctx) &&
IsGetRequest(httpRequest.HttpMethod, httpRequest.Url.AbsolutePath) == false)
{
var requestUrl = ctx.GetRequestUrl();
if (NeverSecret.Urls.Contains(requestUrl, StringComparer.InvariantCultureIgnoreCase))
return true;
ctx.SetStatusToUnauthorized();
return false;
}
if (IsInvalidUser(ctx) == false)
CurrentOperationContext.Headers.Value[Constants.RavenAuthenticatedUser] = ctx.User.Identity.Name;
return true;
}
示例3: IsInvalidUser
private bool IsInvalidUser(IHttpContext ctx, out Action onRejectingRequest)
{
var invalidUser = (ctx.User == null ||
ctx.User.Identity.IsAuthenticated == false);
if (invalidUser)
{
onRejectingRequest = () => ctx.SetStatusToForbidden();
return true;
}
onRejectingRequest = () => ctx.SetStatusToUnauthorized();
if (requiredGroups.Count > 0 || requiredUsers.Count > 0)
{
if (requiredGroups.Any(requiredGroup => ctx.User.IsInRole(requiredGroup)) ||
requiredUsers.Any(requiredUser => string.Equals(ctx.User.Identity.Name, requiredUser, StringComparison.InvariantCultureIgnoreCase)))
return false;
return true;
}
return false;
}
示例4: Respond
public override void Respond(IHttpContext context)
{
if (context.User.IsAdministrator() == false)
{
context.SetStatusToUnauthorized();
context.WriteJson(new
{
Error = "Only administrators can look at the server stats"
});
return;
}
if(ResourceStore != DefaultResourceStore)
{
context.SetStatusToBadRequest();
context.WriteJson(new
{
Error = "Admin stats can only be had from the root database"
});
return;
}
context.WriteJson(server.Statistics);
}
示例5: Authorize
public override bool Authorize(IHttpContext ctx)
{
if (server.DefaultConfiguration.AnonymousUserAccessMode == AnonymousUserAccessMode.None && IsInvalidUser(ctx))
{
ctx.SetStatusToUnauthorized();
return false;
}
IHttpRequest httpRequest = ctx.Request;
if (server.DefaultConfiguration.AnonymousUserAccessMode == AnonymousUserAccessMode.Get &&
IsInvalidUser(ctx) &&
IsGetRequest(httpRequest.HttpMethod, httpRequest.Url.AbsolutePath) == false)
{
ctx.SetStatusToUnauthorized();
return false;
}
return true;
}
示例6: Respond
public override void Respond(IHttpContext context)
{
if (context.User.IsAdministrator() == false)
{
context.SetStatusToUnauthorized();
context.WriteJson(new
{
Error = "Only administrators can stop indexing"
});
return;
}
Database.StopBackgroundWorkers();
}
示例7: Respond
public override void Respond(IHttpContext context)
{
if (context.User.IsAdministrator() == false)
{
context.SetStatusToUnauthorized();
context.WriteJson(new
{
Error = "Only administrators can do that"
});
return;
}
Database.RunIdleOperations();
}
示例8: Respond
public override void Respond(IHttpContext context)
{
if (context.User.IsAdministrator(server.SystemConfiguration.AnonymousUserAccessMode) == false && context.User.IsAdministrator(Database) == false)
{
context.SetStatusToUnauthorized();
context.WriteJson(new
{
Error = "The operation '" + context.GetRequestUrl() +"' is only available to administrators"
});
return;
}
RespondToAdmin(context);
}
示例9: Respond
public override void Respond(IHttpContext context)
{
if(context.IsAdministrator() == false)
{
context.SetStatusToUnauthorized();
context.WriteJson(new
{
Error = "Only administrators can initiate a backup procedure"
});
return;
}
var backupRequest = context.ReadJsonObject<BackupRequest>();
Database.StartBackup(backupRequest.BackupLocation);
context.SetStatusToCreated(BackupStatus.RavenBackupStatusDocumentKey);
}
示例10: Respond
public override void Respond(IHttpContext context)
{
if (context.User.IsAdministrator() == false)
{
context.SetStatusToUnauthorized();
context.WriteJson(new
{
Error = "Only administrators can initiate a database compact procedure"
});
return;
}
if(DefaultResourceStore != ResourceStore)
{
context.SetStatusToBadRequest();
context.WriteJson(new
{
Error = "Compact request can only be issued from the system database"
});
return;
}
var db = context.Request.QueryString["database"];
if(string.IsNullOrWhiteSpace(db))
{
context.SetStatusToBadRequest();
context.WriteJson(new
{
Error = "Compact request requires a valid database parameter"
});
return;
}
var configuration = server.CreateTenantConfiguration(db);
if (configuration == null)
{
context.SetStatusToNotFound();
context.WriteJson(new
{
Error = "No database named: " + db
});
return;
}
server.LockDatabase(db, () =>
DefaultResourceStore.TransactionalStorage.Compact(configuration));
}
示例11: Respond
public override void Respond(IHttpContext context)
{
if (context.User.IsAdministrator() == false)
{
context.SetStatusToUnauthorized();
context.WriteJson(new
{
Error = "Only administrators can initiate a backup procedure"
});
return;
}
var backupRequest = context.ReadJsonObject<BackupRequest>();
var incrementalString = context.Request.QueryString["incremental"];
bool incrementalBackup;
if (bool.TryParse(incrementalString, out incrementalBackup) == false)
incrementalBackup = false;
Database.StartBackup(backupRequest.BackupLocation, incrementalBackup);
context.SetStatusToCreated(BackupStatus.RavenBackupStatusDocumentKey);
}
示例12: Respond
public override void Respond(IHttpContext context)
{
if (context.IsAdministrator() == false)
{
context.SetStatusToUnauthorized();
context.WriteJson(new
{
Error = "Only administrators can start indexing"
});
return;
}
var concurrency = context.Request.QueryString["concurrency"];
if(string.IsNullOrEmpty(concurrency)==false)
{
Database.Configuration.MaxNumberOfParallelIndexTasks = Math.Max(1, int.Parse(concurrency));
}
Database.SpinBackgroundWorkers();
}
示例13: Respond
public override void Respond(IHttpContext context)
{
if (context.Request.ContentLength > MaxOAuthContentLength)
{
context.SetStatusToBadRequest();
context.WriteJson(new { error = "invalid_request", error_description = "Content length should not be over " + MaxOAuthContentLength + " bytes" });
return;
}
if (context.Request.ContentLength == 0)
{
RespondWithChallenge(context);
return;
}
string requestContents;
using (var reader = new StreamReader(context.Request.InputStream))
requestContents = reader.ReadToEnd();
var requestContentsDictionary = OAuthHelper.ParseDictionary(requestContents);
var rsaExponent = requestContentsDictionary.GetOrDefault(OAuthHelper.Keys.RSAExponent);
var rsaModulus = requestContentsDictionary.GetOrDefault(OAuthHelper.Keys.RSAModulus);
if (rsaExponent == null || rsaModulus == null ||
!rsaExponent.SequenceEqual(OAuthServerHelper.RSAExponent) || !rsaModulus.SequenceEqual(OAuthServerHelper.RSAModulus))
{
RespondWithChallenge(context);
return;
}
var encryptedData = requestContentsDictionary.GetOrDefault(OAuthHelper.Keys.EncryptedData);
if(string.IsNullOrEmpty(encryptedData))
{
RespondWithChallenge(context);
return;
}
var challengeDictionary = OAuthHelper.ParseDictionary(OAuthServerHelper.DecryptAsymmetric(encryptedData));
var apiKeyName = challengeDictionary.GetOrDefault(OAuthHelper.Keys.APIKeyName);
var challenge = challengeDictionary.GetOrDefault(OAuthHelper.Keys.Challenge);
var response = challengeDictionary.GetOrDefault(OAuthHelper.Keys.Response);
if (string.IsNullOrEmpty(apiKeyName) || string.IsNullOrEmpty(challenge) || string.IsNullOrEmpty(response))
{
RespondWithChallenge(context);
return;
}
var challengeData = OAuthHelper.ParseDictionary(OAuthServerHelper.DecryptSymmetric(challenge));
var timestampStr = challengeData.GetOrDefault(OAuthHelper.Keys.ChallengeTimestamp);
if(string.IsNullOrEmpty(timestampStr))
{
RespondWithChallenge(context);
return;
}
var challengeTimestamp = OAuthServerHelper.ParseDateTime(timestampStr);
if (challengeTimestamp + MaxChallengeAge < SystemTime.UtcNow || challengeTimestamp > SystemTime.UtcNow)
{
// The challenge is either old or from the future
RespondWithChallenge(context);
return;
}
var apiKeyTuple = GetApiKeySecret(apiKeyName);
if (apiKeyTuple == null)
{
context.SetStatusToUnauthorized();
context.WriteJson(new { error = "unauthorized_client", error_description = "Unknown API Key" });
return;
}
var apiSecret = apiKeyTuple.Item1;
if (string.IsNullOrEmpty(apiKeyName))
{
context.SetStatusToUnauthorized();
context.WriteJson(new { error = "unauthorized_client", error_description = "Invalid API Key" });
return;
}
var expectedResponse = OAuthHelper.Hash(string.Format(OAuthHelper.Keys.ResponseFormat, challenge, apiSecret));
if (response != expectedResponse)
{
context.SetStatusToUnauthorized();
context.WriteJson(new { error = "unauthorized_client", error_description = "Invalid challenge response" });
return;
}
var token = apiKeyTuple.Item2;
context.Write(token.Serialize());
}
示例14: TryCreateUser
private bool TryCreateUser(IHttpContext ctx, string databaseName, out Action onRejectingRequest)
{
var invalidUser = (ctx.User == null || ctx.User.Identity.IsAuthenticated == false);
if (invalidUser)
{
onRejectingRequest = () =>
{
ProvideDebugAuthInfo(ctx, new
{
Reason = "User is null or not authenticated"
});
ctx.Response.AddHeader("Raven-Required-Auth", "Windows");
if (string.IsNullOrEmpty(Settings.OAuthTokenServer) == false)
{
ctx.Response.AddHeader("OAuth-Source", Settings.OAuthTokenServer);
}
ctx.SetStatusToUnauthorized();
};
return false;
}
var dbUsersIaAllowedAccessTo = requiredUsers
.Where(data => ctx.User.Identity.Name.Equals(data.Name, StringComparison.InvariantCultureIgnoreCase))
.SelectMany(source => source.Databases)
.Concat(requiredGroups.Where(data => ctx.User.IsInRole(data.Name)).SelectMany(x => x.Databases))
.ToList();
var user = UpdateUserPrincipal(ctx, dbUsersIaAllowedAccessTo);
onRejectingRequest = () =>
{
ctx.SetStatusToForbidden();
ProvideDebugAuthInfo(ctx, new
{
user.Identity.Name,
user.AdminDatabases,
user.ReadOnlyDatabases,
user.ReadWriteDatabases,
DatabaseName = databaseName
});
};
return true;
}
示例15: AssertSecurityRights
private bool AssertSecurityRights(IHttpContext ctx)
{
if (DefaultConfiguration.AnonymousUserAccessMode == AnonymousUserAccessMode.None && IsInvalidUser(ctx))
{
ctx.SetStatusToUnauthorized();
return false;
}
if (DefaultConfiguration.AnonymousUserAccessMode == AnonymousUserAccessMode.Get && IsInvalidUser(ctx) && IsNotGetRequest(ctx) )
{
ctx.SetStatusToUnauthorized();
return false;
}
return true;
}