Home >Backend Development >C++ >How to implement user authentication and authorization in C++?

How to implement user authentication and authorization in C++?

WBOY
WBOYOriginal
2024-06-03 22:35:09743browse

Implementing user authentication and authorization in C++ involves the following steps: Securely storing usernames and passwords, and hashing passwords. Verify the user's password when they log in and allow access to the application. Grant users different capabilities based on their roles or permissions.

How to implement user authentication and authorization in C++?

How to implement user authentication and authorization in C++

User authentication and authorization is to ensure application security and user data critical steps for confidentiality. Implementing these features in C++ involves the following steps:

1. Username and Password Storage

Securely store usernames and passwords in a database using an encryption algorithm such as SHA -256) Hash the password.

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
    // 打开数据库文件
    ifstream database("users.db");

    // 读取用户名和密码对
    string line;
    while (getline(database, line))
    {
        // 使用逗号分隔符将行拆分为用户名和密码
        stringstream ss(line);
        string username, password;
        getline(ss, username, ',');
        getline(ss, password);

        // 将密码进行 SHA-256 哈希处理
        string hashedPassword = sha256(password);

        // 将哈希后的密码和用户名存储在数据库中
        ofstream output("users.db", ios::out | ios::app);
        output << username << "," << hashedPassword << endl;
    }

    return 0;
}

2. User Authentication

When the user attempts to log in, the entered password is compared to the hashed password in the database and access is allowed.

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;

int main()
{
    // 获取用户的用户名和密码
    string username, password;
    cout << "Enter username: ";
    cin >> username;
    cout << "Enter password: ";
    cin >> password;

    // 从数据库中读取哈希密码
    string hashedPassword;
    ifstream database("users.db");
    while (getline(database, line))
    {
        // 将行拆分为用户名和密码
        stringstream ss(line);
        string dbUsername, dbPassword;
        getline(ss, dbUsername, ',');
        getline(ss, dbPassword);

        // 检查用户名是否匹配
        if (username == dbUsername)
        {
            hashedPassword = dbPassword;
            break;
        }
    }

    // 比较输入的密码和数据库中的哈希密码
    string inputHashedPassword = sha256(password);
    if (inputHashedPassword == hashedPassword)
    {
        cout << "Authentication successful" << endl;
    }
    else
    {
        cout << "Authentication failed" << endl;
    }

    return 0;
}

3. User authorization

Grant users different functions based on their roles or permissions.

#include <iostream>
#include <map>

using namespace std;

int main()
{
    // 创建一个保存角色和授权的映射
    map<string, string> roles = {
        {"admin", "READ, WRITE, DELETE"},
        {"user", "READ, WRITE"},
        {"viewer", "READ"}
    };

    // 获取用户的角色
    string role;
    cout << "Enter your role: ";
    cin >> role;

    // 检查用户的授权
    if (roles.find(role) == roles.end())
    {
        cout << "Invalid role" << endl;
    }
    else
    {
        // 获取用户的授权并打印出来
        string permissions = roles[role];
        cout << "Permissions: " << permissions << endl;
    }

    return 0;
}

By implementing these steps, you can build a secure user authentication and authorization system in your C++ application.

The above is the detailed content of How to implement user authentication and authorization in C++?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn