I work on asp.net mvc application csharp . i face issue i can't get user name windows who submit click on my web application so on my application i have user details page have submit button when i click on submit button i will get current windows logged in and insert it on database so How to get current windows user that submit click on user details page my web application hosted on IIS . so are there are any way to get current windows user logged on my app so can submit user details example scenario suppose i work on asp.net MVC web application register and add employee who will resignation from company so when user open his page and he need to insert his resignation related to it so i will take windows user logged in and add it on submit page so i will know which user submit application if any way can get user logged to system depend on windows credential so i can register on database as logging so after that i will know which user submit resignation form .
Loading
Jithu ThomasPosted Jan 24, 2024, 11:31 AM
here is an example: -
ahmed salahPosted Jan 24, 2024, 10:01 AM
are authorization required
if yes how to add on web.config please
Jithu ThomasPosted Jan 24, 2024, 9:40 AM
Code on the back end: -
using System.Security.Principal;
public class YourController : Controller
{
public ActionResult SubmitDetails()
{
// Get the current Windows user identity
IPrincipal user = HttpContext.Current.User;
// Check if the user is authenticated
if (user != null && user.Identity != null && user.Identity.IsAuthenticated)
{
// Get the Windows username
string windowsUsername = user.Identity.Name;
// Now you can use windowsUsername to store it in your database or perform other actions
// For example, you can save it in your resignation form submission
// dbContext.SaveResignationForm(windowsUsername, otherFormDetails);
// Redirect or return the appropriate view
return RedirectToAction("Index", "Home");
}
else
{
// Handle the case where the user is not authenticated
// This could happen in case of an anonymous user or an issue with authentication
return RedirectToAction("Login", "Account");
}
}
}
on web.config configuration file.
ahmed salahPosted Jan 24, 2024, 7:29 AM
what setting i will do on iis or web.config
all pc will have logged user windows in AD
Jayraj ChhayaPosted Jan 24, 2024, 6:55 AM
To get the current Windows user logged in to your ASP.NET MVC application, you can use the
WindowsIdentityclass from theSystem.Security.Principalnamespace. Here's an example of how you can retrieve the current Windows user:Please note that in order to retrieve the Windows user, your application needs to be running with Windows authentication enabled. Additionally, the user's browser must be configured to allow Windows authentication.