Hi
I need 'I trust this device. Don't ask code for next time' option at the login time using c# code. Every time we our clients used to enter the OTP at the login time. They want this option if they check this checkbox and login they will not ask for OTP again on that device. Please any suggestions or solutions for this uding c# code. Please see the SNIP below as sample i need like this



Mohammad HussainRank #513 · 4.8K reputationPosted Aug 28, 2023, 3:14 AM EDT
// During login void HandleLogin() { // Perform user authentication // Generate a session token and store it in a secure manner (e.g., database) if (UserCheckedTrustDeviceCheckbox) { // Set a persistent cookie with the session token and expiration date HttpCookie cookie = new HttpCookie("TrustedDeviceCookie"); cookie.Values["sessionToken"] = generatedSessionToken; cookie.Expires = DateTime.Now.AddYears(1); // Adjust the expiration as needed Response.Cookies.Add(cookie); } } // On subsequent requests (e.g., in a filter or middleware) void HandleSubsequentRequest() { HttpCookie cookie = Request.Cookies["TrustedDeviceCookie"]; if (cookie != null) { // Retrieve session token from the cookie string sessionToken = cookie.Values["sessionToken"]; // Look up the user and device details using the session token // If the user and device are trusted, skip the OTP step } }Cr BhargaviRank #420 · 5.9K reputationPosted Aug 28, 2023, 4:50 AM EDT
rontend Changes: Modify your login form to include a "Remember Me" or "Trust this device" checkbox. When the user checks this box, you'll set a persistent cookie on the user's device.
Backend Changes: When the user logs in and selects the "Remember Me" option, the backend should generate a unique token associated with the user's device and save it in the database along with the user's ID.
Authentication Flow: During subsequent login attempts, the backend should first check for the presence of the persistent cookie on the user's device. If the cookie is found, it can skip the OTP step and log the user in directly.
Security Considerations:
Amit MohantyRank #112 · 20K reputationPosted Aug 28, 2023, 6:38 AM EDT
Example:
public bool AuthenticateUser(string username, string password, bool rememberDevice) { // Check username and password against the database // ... // Check if the device is trusted bool isDeviceTrusted = IsDeviceTrusted(username); if (isDeviceTrusted) { // Log the user in without OTP // ... } else { // Prompt for OTP // ... // Mark this device as trusted if the check box is selected if(rememberDevice) { MarkDeviceAsTrusted(username, deviceId); } } } public void MarkDeviceAsTrusted(string username, string deviceId) { // Update the database to mark the device as trusted // ... } public bool IsDeviceTrusted(string username) { // Check if the current device is trusted for the user // ... }