Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

session #3

Open
SOunit opened this issue Mar 21, 2024 · 7 comments
Open

session #3

SOunit opened this issue Mar 21, 2024 · 7 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@SOunit
Copy link
Owner

SOunit commented Mar 21, 2024

クライアント

  • リクエストを行う
  • サーバーからクッキーを受け取る。
  • クッキーにはセッションIDが含まれている。
  • リクエスト時、クッキーはサーバーへ自動送信される。
  • セッションを使うことで、ユーザーユニークなページや情報を受け取れる

サーバー

  • リクエストを受け取る
  • クッキーを生成して、セッションIDを追加、ブラウザに渡す
  • セッションIDを確認することで、ユーザーの識別が可能になる(ステートフル)
  • セッション変数に情報を保存できる
  • セッション変数の保存は、サーバー側のメモリで行われる
  • セッション変数の保存は、DBやredisに変更も可能
  • セッションID、キー、バリューという形で情報の保存が可能
    • セッションIDがユーザーユニークを保証
    • その下のキー、バリューのセットで、複数の変数の保存が可能

注意点

  • セッション変数のクリアタイミングを考慮する
  • セッションIDを奪えば、なりすましが可能
@SOunit SOunit added the documentation Improvements or additions to documentation label Mar 21, 2024
@SOunit SOunit self-assigned this Mar 21, 2024
@SOunit
Copy link
Owner Author

SOunit commented Mar 21, 2024

2fa validate

        [NonAction]
        protected (bool IsValid, CommunicationType Type, bool isTimedOut) Is2FAValid(string code)
        {
            // get session values
            var sessionCode = HttpContext.Session.GetString(SESSION_2FA_SETUP_KEY);
            var sessionTypeStr = HttpContext.Session.GetString(SESSION_2FA_SETUP_TYPE_KEY);
            var sessionCreatedStr = HttpContext.Session.GetString(SESSION_2FA_CREATED_KEY);

            // check if session values
            if (sessionCode.IsNullOrEmpty() || sessionTypeStr.IsNullOrEmpty() || sessionCreatedStr.IsNullOrEmpty())
                return (false, CommunicationType.SMS, false);

            // get values from session
            var sessionType = (CommunicationType)int.Parse(sessionTypeStr);
            var sessionCreated = DateTime.Parse(sessionCreatedStr);

            // check time
            var isTimedOut = DateTime.UtcNow > (sessionCreated.AddMinutes(1));

            // validate 4 digit value with the value in session
            var isValid = (sessionCode == code && !isTimedOut) || (this.AppSettings.IsDebug && code == "0000");
            if (isValid)
            {
                HttpContext.Session.Remove(SESSION_2FA_SETUP_KEY);
                HttpContext.Session.Remove(SESSION_2FA_SETUP_TYPE_KEY);
                HttpContext.Session.Remove(SESSION_2FA_CREATED_KEY);
            }

            return (isValid, sessionType, isTimedOut);
        }

@SOunit
Copy link
Owner Author

SOunit commented Mar 21, 2024

2fa generate

        [NonAction]
        protected bool Send2FA(User dbUser, CommunicationType? communicationType)
        {
            if (!communicationType.HasValue || communicationType == CommunicationType.None)
                return false;

            var twoFactorCode = this.AccountService.Send2FA(dbUser, communicationType.Value);
            HttpContext.Session.SetString(SESSION_2FA_SETUP_KEY, twoFactorCode);
            HttpContext.Session.SetString(SESSION_2FA_SETUP_TYPE_KEY, ((int)communicationType).ToString());
            HttpContext.Session.SetString(SESSION_2FA_CREATED_KEY, DateTime.UtcNow.ToString());
            return true;
        }

@SOunit
Copy link
Owner Author

SOunit commented Mar 21, 2024

how asp.net core mvc session work

Sessions in ASP.NET Core are used to store user-specific data across multiple requests. Each user is identified by a unique session ID, which is typically stored in a cookie or passed as part of the request. Sessions can be used to store user authentication tokens, user preferences, shopping cart contents, and other user-specific data.

In your code examples, sessions are used to store temporary data related to two-factor authentication (2FA). Here's a brief overview of how sessions work in your code:

  1. Storing 2FA Setup Data:

    • When the Send2FA method is called, it generates a 2FA code for the user and stores it, along with the communication type and the timestamp when the code was created, in the session.
    • The SESSION_2FA_SETUP_KEY, SESSION_2FA_SETUP_TYPE_KEY, and SESSION_2FA_CREATED_KEY keys are used to store this data in the session.
  2. Validating 2FA Code:

    • When the Is2FAValid method is called to validate a 2FA code entered by the user, it retrieves the stored session data using the same keys.
    • It compares the entered code with the stored code and checks if the session has expired (more than 1 minute has passed since the code was created).
    • If the code is valid and the session has not expired, the method returns true and removes the session data. Otherwise, it returns false.

Sessions work for multiple users because each user's session is unique and maintained separately by the server. When a user makes a request, the server retrieves the session data associated with that user's session ID. This allows each user to have their own set of session data, independent of other users.

It's important to note that sessions typically rely on cookies to store the session ID. This means that sessions will not work if cookies are disabled in the user's browser. Additionally, session data is stored in memory by default, which may not scale well for large numbers of users or in distributed server environments. You can configure ASP.NET Core to use other session storage options, such as databases or distributed caches, for better scalability.

@SOunit
Copy link
Owner Author

SOunit commented Mar 21, 2024

セッション変数とは?【分かりやすい解説シリーズ #17】【プログラミング】

https://www.youtube.com/watch?v=HZ_CQvQICmU

@SOunit
Copy link
Owner Author

SOunit commented Mar 21, 2024

セッション情報はサーバーのどこに保存される?

セッション情報は通常、サーバー側のメモリやデータベースなどの一時的なストレージに保存されます。セッション情報は、ユーザーがウェブサイトにアクセスしている間、そのセッションに関する情報を保持するために使用されます。たとえば、ユーザーがログインすると、サーバーはそのユーザーが認証されていることを示すセッション情報を保存します。ユーザーがログアウトするか、一定の期間が経過すると、セッション情報は通常クリアされます。

@SOunit
Copy link
Owner Author

SOunit commented Mar 21, 2024

第11章 クッキーとセッションを利用しよう

https://kanda-it-school-kensyu.com/php-basic-contents/pb_ch11/pb_1103

@SOunit
Copy link
Owner Author

SOunit commented Mar 21, 2024

Node.js Expressの例を通じて、Webアプリのセッションについて理解する

https://kanda-it-school-kensyu.com/php-basic-contents/pb_ch11/pb_1103

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant