views:

339

answers:

1

MyMasterPage.master

<head runat="server">
    <script type="text/javascript">

    $(document).ready(function() {

        var userName = '<%# System.Web.HttpContext.Current.Request.ServerVariables["AUTH_USER"].ToString() %>';
        alert(userName);
        $.ajax({ type: "POST",
            url: "DemoWebService.asmx/GetFulName",
            contentType: "application/json; charset=utf-8",
            dataType: "xml",
            dataType: "json",
            data: "{'networkId':'" + userName + "'}",
            processData: false,
            error: function(XMLHttpRequest, textStatus, errorThrown) { ajaxError(XMLHttpRequest, textStatus, errorThrown); },
            success: function(xml) { ajaxFinish(xml); }
        });

    });
  </script>
</head>

Even though the userName variable is blank, I get the logged in user from my WebMethod:

[WebMethod]
public string GetFulName(string networkId)
{
    //return networkId + " Full Name";
    return networkId + " From Server: " + System.Web.HttpContext.Current.Request.ServerVariables["AUTH_USER"].ToString();
}

Since I am checking on the master page, what is the best practice to store the authentication result so that I do not have to check on everypage. Prior to using jquery I was storing in Session.

Thanks

web.config

<authentication mode="Windows"/>
<webServices>
<protocols>
  <add name="HttpPost"/>
  <add name="HttpGet"/>
</protocols>
</webServices>
+1  A: 

You could store the result in a cookie, and then check that cookie on subsequent pages to get the logged in user's name. You would want to make sure and delete the cookie if the user logged out or their authenticated session expired.

There is a plugin available for jQuery to support reading/writing/deleting cookies.

Joshua Poehls