当前位置: 首页>>代码示例>>C#>>正文


C# IHttpContext.SetStatusToUnauthorized方法代码示例

本文整理汇总了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;
		}
开发者ID:nicolasgarfinkiel,项目名称:ravendb,代码行数:28,代码来源:WindowsRequestAuthorizer.cs

示例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;
		}
开发者ID:jdaigle,项目名称:ravendb,代码行数:30,代码来源:WindowsRequestAuthorizer.cs

示例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;
		}
开发者ID:neiz,项目名称:ravendb,代码行数:25,代码来源:WindowsRequestAuthorizer.cs

示例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);

		}
开发者ID:royra,项目名称:ravendb,代码行数:25,代码来源:AdminStats.cs

示例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;
		}
开发者ID:quenth,项目名称:ravendb,代码行数:20,代码来源:WindowsRequestAuthorizer.cs

示例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();
		}
开发者ID:neiz,项目名称:ravendb,代码行数:14,代码来源:AdminStopIndexing.cs

示例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();
		}
开发者ID:neiz,项目名称:ravendb,代码行数:14,代码来源:AdminRunIdleOperations.cs

示例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);
		}
开发者ID:jjchiw,项目名称:ravendb,代码行数:14,代码来源:AdminResponder.cs

示例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);
		}
开发者ID:JPT123,项目名称:ravendb,代码行数:16,代码来源:AdminBackup.cs

示例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));
		}
开发者ID:royra,项目名称:ravendb,代码行数:47,代码来源:AdminCompact.cs

示例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);
		}
开发者ID:Korn1699,项目名称:ravendb,代码行数:20,代码来源:AdminBackup.cs

示例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();
		}
开发者ID:JPT123,项目名称:ravendb,代码行数:21,代码来源:AdminStartIndexing.cs

示例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());
		}
开发者ID:synhershko,项目名称:ravendb,代码行数:89,代码来源:OAuthApiKeyResponder.cs

示例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;
		}
开发者ID:925coder,项目名称:ravendb,代码行数:43,代码来源:WindowsRequestAuthorizer.cs

示例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;
        }
开发者ID:nzdunic,项目名称:ravendb,代码行数:16,代码来源:HttpServer.cs


注:本文中的IHttpContext.SetStatusToUnauthorized方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。