使用jquery对Windows用户进行身份验证

时间:2009-03-13 22:23:33

标签: asp.net jquery web-services

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>

即使userName变量为空,我也从WebMethod获取登录用户:

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

由于我正在检查母版页,因此存储认证结果的最佳做法是什么,这样我就不必检查每个页面。在使用jquery之前,我在Session中存储。

由于

的web.config

<authentication mode="Windows"/>
<webServices>
<protocols>
  <add name="HttpPost"/>
  <add name="HttpGet"/>
</protocols>
</webServices>

1 个答案:

答案 0 :(得分:1)

您可以将结果存储在cookie中,然后在后续页面上检查该cookie以获取登录用户的名称。如果用户已注销或其认证会话已过期,您可能需要确保并删除co​​okie。

plugin available for jQuery支持读/写/删除Cookie。

相关问题