#include
#include
#include
// Structure for User
typedef struct {
char username[50];
char password[50];
char role[10]; // Admin, User, or Guest
} User;
// Predefined users (In real systems, use a database)
User users[] = {
{"admin", "admin123", "Admin"},
{"user1", "user123", "User"},
{"guest1", "guest123", "Guest"}
};
int user_count = 3;
User *logged_in_user = NULL;
// Function to authenticate user
int authenticate(char *username, char *password) {
for (int i = 0; i < user_count; i++) {
if (strcmp(users[i].username, username) == 0 && strcmp(users[i].password, password) == 0) {
logged_in_user = &users[i];
return 1;
}
}
return 0;
}
// Function to check access permissions
int check_access(char *resource) {
if (strcmp(logged_in_user->role, "Admin") == 0) {
return 1; // Admins can access all resources
}
else if (strcmp(logged_in_user->role, "User") == 0 && strcmp(resource, "public") == 0) {
return 1; // Users can access public resources
}
else if (strcmp(logged_in_user->role, "Guest") == 0 && strcmp(resource, "guest") == 0) {
return 1; // Guests can access guest resources
}
return 0;
}
// Function to access resource
void access_resource(char *resource) {
printf("\nAttempting to access: %s\n", resource);
if (check_access(resource)) {
printf("Access Granted to %s\n", resource);
} else {
printf("Access Denied to %s\n", resource);
}
}
// Main Menu
void menu() {
int choice;
char resource[20];
while (1) {
printf("\n--- Access Control System ---\n");
printf("1. Access Public Resource\n");
printf("2. Access Admin Resource\n");
printf("3. Access Guest Resource\n");
printf("4. Logout\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
access_resource("public");
break;
case 2:
access_resource("admin");
break;
case 3:
access_resource("guest");
break;
case 4:
printf("Logging out...\n");
logged_in_user = NULL;
return;
default:
printf("Invalid choice!\n");
}
}
}
// Main Function
int main() {
char username[50];
char password[50];
printf("--- Welcome to Access Control System ---\n");
while (1) {
printf("\nPlease Login:\n");
printf("Username: ");
scanf("%s", username);
printf("Password: ");
scanf("%s", password);
if (authenticate(username, password)) {
printf("\nLogin Successful! Welcome %s\n", logged_in_user->username);
menu();
} else {
printf("Invalid Credentials! Loading
Daniel WrightPosted Feb 22, 2025, 8:49 AM
Scheduling background jobs in ASP.NET Core using Quartz.NET involves setting up a job scheduler to execute tasks or jobs asynchronously at specified intervals or times. It's like having a system that can perform tasks in the background without directly involving user interaction.
In your provided code snippet, you have a simulated Access Control System where users can log in with specific roles (Admin, User, Guest) and access different resources based on their role. This system showcases the concepts of authentication, role-based access control, and resource authorization.
When it comes to scheduling background jobs with Quartz.NET in ASP.NET Core, you would typically follow these steps:
1. Setting up Quartz.NET: You need to install the Quartz.NET NuGet package and configure it in your ASP.NET Core application. This involves defining jobs, triggers, and the scheduler itself.
2. Defining Jobs: Jobs represent the tasks you want to execute. In your case, it could be functions to perform specific actions in the background.
3. Configuring Triggers: Triggers define when and how often a job should be executed. You can schedule jobs to run at fixed intervals, specific times, or using cron expressions.
4. Starting the Scheduler: Once everything is set up, you start the Quartz.NET scheduler in your application to begin executing the scheduled jobs.
Here's a basic example illustrating a scheduled job using Quartz.NET in ASP.NET Core:
In this example, `ScheduledJob` is a simple job that just logs the current time. The `JobScheduler` class sets up this job to execute every 10 seconds.
To use this scheduler in your ASP.NET Core application, you would typically initialize it in your `Startup.cs` or any appropriate service, ensuring it starts when your application starts.
Remember, with Quartz.NET, you can implement sophisticated scheduling scenarios, handle job persistence, and scale your background job processing efficiently. It's a powerful tool for automating tasks within your ASP.NET Core application.