0

I'm using the following code to check the user's credentials and if successful I put them to make-request.aspx, but on make-request.aspx I want to check the value of the username they entered so I can show certain content.

Here's the authentication code:

foreach (string key in ConfigurationSettings.AppSettings.Keys)
{
    dominName = key.Contains("DirectoryDomain") ? ConfigurationSettings.AppSettings[key] : dominName;
    adPath = key.Contains("DirectoryPath") ? ConfigurationSettings.AppSettings[key] : adPath;

    if (!String.IsNullOrEmpty(dominName) && !String.IsNullOrEmpty(adPath))
    {
        if (true == AuthenticateUser(dominName, userName, txtPassword.Text,adPath, out strError))
        {
            Response.Redirect("../make-request.aspx");// Authenticated user redirects to default.aspx
        }

        dominName = string.Empty;
        adPath = string.Empty;
        if (String.IsNullOrEmpty(strError)) break;
    }

Everything works fine but I'm not sure how to get the username they entered into the form. Here's code that I've tried that is getting username of the machine username -- I think. Any help would be appreciated!

I've tried all three of these:

//string userName = Environment.UserName;

string userName = HttpContext.Current.User.Identity.Name;

//string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

Here's the authentication/auth section of web.config:

<authentication mode="Windows" />
<authorization>
  <allow users="*" />
  <!--<deny users="*"/>-->
</authorization>
user1431633
  • 658
  • 2
  • 15
  • 34

3 Answers3

2

You are authenticating the user but not setting forms authentication cookie. Here's what you need to do:

FormsAuthentication.SetAuthCookie(userName, false);
Response.Redirect("../make-request.aspx");

Also make sure you have proper authentication/authorization set in your web.config. If you are not sure if it is setup correctly, share it here so we can take a look.

Set FormsAuthentication as below:

 <authentication mode="Forms">

      <forms loginUrl="Login.aspx"/>

    </authentication>

    <authorization>

      <deny users="?"/>

    </authorization>
gbs
  • 7,196
  • 5
  • 43
  • 69
  • so how would I get the value of userName here? Something with GetAuthCookie()? – user1431633 Feb 06 '14 at 17:59
  • After you do that the HttpContext.Current.User.Identity.Name should start populating – gbs Feb 06 '14 at 18:10
  • it's not populating but this is starting to make sense. I'll update my question with my web.config if you can continue to help. Which part of web.config would help you diagnose? Thanks! – user1431633 Feb 06 '14 at 18:14
0

The HttpContext.Current.User.Identity.Name will work as long as the user is currently logged in when it is ran. In one of my sites, I use the following (written in VB):

Dim u As MembershipUser = Membership.GetUser(Membership.GetUserNameByEmail(HttpContext.Current.User.Identity.Name))

Tip: You can test if the user is already logged in by checking the value of HttpContext.Current.User.Identity.IsAuthenticated.


However . . .

. . . Using the current HTTP context is only necessary in content pages or web APIs. Alternatively, you can use MembershipUser u = Membership.GetUser(); from the master page, and then use u.Username to retrieve the username or u.ProviderUserKey to retrieve the GUID of the user.

Ross Brasseaux
  • 3,879
  • 1
  • 28
  • 48
-1
     If Session Is Nothing OrElse Session(Current_User) Is Nothing Then
                    udtGeneral = GetdoGeneralInstance()
                    susername = Request.ServerVariables("LOGON_USER").Split("\")(1).ToString()
'Either of these work i believe                    
susername = Request.ServerVariables(7).Split("\")(1).ToString()
                    'Dim susername1 = Request.Browser.Capabilities("extra").ToString.Split(";")(14).ToString.Split(":")(1).ToString
                    Session("ipAddress") = Request.ServerVariables("REMOTE_ADDR").ToString()
                End If
Brandon
  • 915
  • 4
  • 23
  • 44
  • So why didn't you do it? – MikeSmithDev Feb 06 '14 at 18:07
  • PS I didn't downvote your post even though it's wrong language and appears to have nothing to do with the OP. Though oddly enough I just did get a downvote on [an old answer of mine](http://stackoverflow.com/a/13727769/1810243) coincidentally at same time your post was downvoted. Wonder why. – MikeSmithDev Feb 06 '14 at 18:17
  • The OP actually did try multiple times (oops) to run sections of the code and found it unhelpful. But I didn't downvote. – user1431633 Feb 06 '14 at 18:53