Problem
In einer ASP.NET Seite möchte man den Windows-Anmeldebenutzer ermitteln.
Ansatz / Approach
In der Web-Anwendung muss man zunächst den Haken „Enable Anonymous Access“ entfernen und einen Haken bei „Enable Windows Authentication“ setzen.
Lösung / Solution
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for AuthenticationService
/// </summary>
public class AuthenticationService
{
public AuthenticationService()
{
//
// TODO: Add constructor logic here
//
}
public string getUsername()
{
string windowsLogin = System.Web.HttpContext.Current.User.Identity.Name;
if (windowsLogin == null) return "none";
int hasDomain = windowsLogin.IndexOf(@"\");
if (hasDomain > 0)
{
windowsLogin = windowsLogin.Remove(0, hasDomain + 1);
} //end if
return windowsLogin;
}
}