Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am working on mixed authentication mode in ASP.NET MVC 4, and I want the Windows username to be auto filled in my textbox. I have used System.Security.Principal.WindowsIdentity.GetCurrent().Name; and it is working fine on local, but when i am executing it on the server, "Default AppPool " is coming in username textbox.
Please suggest some approch.
thanks in advance

What I have tried:

@model
@{
ViewBag.Title = "Active directory authentication";
}
@{
string UserIDwindows = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
string[] Usernameis = UserIDwindows.Split('\\');
if (ViewData["Error"] != "" && ViewData["Error"] != null)
{
string res = ViewData["Error"].ToString();



@res


ViewData["Error"] = null;

}
}

@using (Html.BeginForm("index", "Activedirectory"))
{

ENTER YOUR NETWORK / SYSTEM LOGIN CREDENTIALS


@Html.LabelFor(a => a.UserID)

@Html.TextBoxFor(a => a.UserID, new { @Value = Usernameis[1] })
@* @Html.TextBox(UserID,)*@

@Html.ValidationMessageFor(a => a.UserID)

@Html.LabelFor(a => a.Password)

@Html.TextBoxFor(a => a.Password)

@Html.ValidationMessageFor(a => a.Password)

@Html.LabelFor(a => a.DomainName)

@Html.TextBoxFor(a => a.DomainName)

@Html.ValidationMessageFor(a => a.DomainName)



}
Posted
Updated 26-Jul-17 5:30am
v3
Comments
F-ES Sitecore 26-Jul-17 6:32am    
The site probably isn't configured to use Windows Authentication, or Windows Auth isn't working (it won't work over the internet for example).
Member 12785164 26-Jul-17 6:46am    
can you please elaborate a little.
my application is intranet application.

1 solution

The reason why you get default app pool is because of System.Security.Principal.WindowsIdentity.GetCurrent().Name.

When you are running locally i am assuming you mean from visual studio which is why you see your username. Your user is running visual studio, which launched IE Express which is how you work with your page.

If you were to move that site to full blown IIS locally on your computer it will probably do the same thing.

This is more than likely because you've enabled anonymous authentication in your app when you need to have it disabled. When you've got it out on IIS go to the site in IIS, click the Authentication icon under <site name> Home. Then you should see a listing of auth types. You'll see Anonymous Authentication is probably set to enable and everything else is Disabled. You'll want to disable anonymous auth and enable windows auth.

Then, to access user info you should be able to use User.Identity.***..for example User.Identity.UserName or something like that.

It has been a while since i've done windows auth in MVC so I think you need to look some links for more clarification.

How To: Use Windows Authentication in ASP.NET 2.0[^]
 
Share this answer
 
Comments
Member 12785164 27-Jul-17 1:24am    
i am not able to use only in windows authentication 'enable' if i do enable it and disable all other then 'Access is denied' error occurs .And User.Identity.UserName gives null back.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900