{"id":20411848,"url":"https://github.com/markopapic/twofactorauthentication","last_synced_at":"2026-04-19T11:31:47.083Z","repository":{"id":143393476,"uuid":"300078287","full_name":"MarkoPapic/TwoFactorAuthentication","owner":"MarkoPapic","description":"A C# library for 2-factor authentication.","archived":false,"fork":false,"pushed_at":"2020-09-30T23:22:59.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-29T01:03:45.584Z","etag":null,"topics":["2fa","rfc-6238","security","totp","two-factor-authentication"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MarkoPapic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-30T22:27:12.000Z","updated_at":"2022-08-22T05:22:20.000Z","dependencies_parsed_at":"2023-09-16T16:19:06.802Z","dependency_job_id":null,"html_url":"https://github.com/MarkoPapic/TwoFactorAuthentication","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarkoPapic%2FTwoFactorAuthentication","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarkoPapic%2FTwoFactorAuthentication/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarkoPapic%2FTwoFactorAuthentication/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarkoPapic%2FTwoFactorAuthentication/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MarkoPapic","download_url":"https://codeload.github.com/MarkoPapic/TwoFactorAuthentication/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241955639,"owners_count":20048504,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["2fa","rfc-6238","security","totp","two-factor-authentication"],"created_at":"2024-11-15T05:54:12.282Z","updated_at":"2026-04-19T11:31:47.011Z","avatar_url":"https://github.com/MarkoPapic.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Two-Factor Authentication\n\n[![NuGet Version](https://img.shields.io/nuget/v/MarkoPapic.TwoFactorAuthentication.svg)](https://www.nuget.org/packages/MarkoPapic.TwoFactorAuthentication/)\n\nA C# library for TOTP ([RFC 6238](https://tools.ietf.org/html/rfc6238)) 2-factor authentication.\n\n## Installation\n\nVisual Studio Package Manager Console:\n\n```\nInstall-Package MarkoPapic.TwoFactorAuthentication\n```\n\ndotnet CLI:\n\n```\ndotnet add package MarkoPapic.TwoFactorAuthentication\n```\n\n\n## Usage\n\n\n### Authenticator app\n\nTo generate a new authenticator app key:\n```csharp\nTwoFactorAuthenticationManager twoFactorAuthenticationManager = new TwoFactorAuthenticationManager();\nstring authenticatorKey = twoFactorAuthenticationManager.AuthenticatorApp.GenerateKey();\n```\n\nThe `authenticatorKey` should be entered in an authenticator app.\nThen, to validate a code generated by an authenticator app:\n\n```csharp\nbool isCodeValid = twoFactorAuthenticationManager.AuthenticatorApp.ValidateCode(key, code);\n```\nWhere `code` it the code generated by an authenticator app.\n\n### Message based\n\nTo generate a TOTP to be sent to the user (via SMS, email...):\n```\nTwoFactorAuthenticationManager twoFactorAuthenticationManager = new TwoFactorAuthenticationManager();\nstring totp = twoFactorAuthenticationManager.Message.GenerateTotp(userKey);\n```\nWhere `userKey` is a Base32 encoded string that is uniquely associated to this user. This key should be provided by you.\nThen, to validate the TOTP sent by the user:\n```\nbool isTotpValid = twoFactorAuthenticationManager.Message.ValidateCode(userKey, totp);\n```\nWhere `userKey` is the same key you used to generate the TOTP, and `totp` is the TOTP code generated in the previous step.\n\n## Configuration\n\nYou can configure the following parameters:\n* `MessageTotpDuration`: The duration for which message-based TOTPs should be valid. Default is 300 seconds.\n* `AuthenticatorTotpVarianceAllowed`: Allows up to the specified adjacent intervals to be checked when validating authenticator app TOTPs. This can make up for delays caused by latency or clock missmatch. Default is 0.\n* `MessageTotpVarianceAllowed`: Allows up to the specified adjacent intervals to be checked when validating message-based TOTPs. This can make up for delays caused by latency or clock missmatch. Default is 0.\n\nExample:\n```csharp\nTwoFactorAuthenticationManager twoFactorAuthenticationManager = new TwoFactorAuthenticationManager(new TwoFactorAuthenticationOptions\n    {\n        MessageTotpDuration = 500,\n        MessageTotpVarianceAllowed = 1,\n        AuthenticatorTotpVarianceAllowed = 0\n    });\n```\n\n### Using .Net Core Dependency Injection\n\nYou can use the .NET Core middleware to register `TwoFactorAuthenticationManager` as a service available via .NET dependency injection:\n\n```\npublic void ConfigureServices(IServiceCollection services)\n{\n   // ...\n\n    services.AddTwoFactorAuthentication();\n\n    services.Configure\u003cTwoFactorAuthenticationOptions\u003e(options =\u003e\n    {\n        options.MessageTotpDuration = 500;\n    });\n    \n    // ...\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkopapic%2Ftwofactorauthentication","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarkopapic%2Ftwofactorauthentication","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkopapic%2Ftwofactorauthentication/lists"}