2009-03-13 5 views
1

Comment obtient-on le nom d'utilisateur connecté à l'aide de jquery?Compte de domaine connecté jquery

J'utilise ASP.Net et ai fait dans le code derrière comme celui-ci:

System.Web.HttpContext.Current.Request.ServerVariables["AUTH_USER"].ToString(); 

Je suis en train d'authentifier un utilisateur en utilisant le nom de connexion de domaine et utiliser un service Web pour authentifier.

<script type="text/javascript"> 
    //<![CDATA[ 
$(document).ready(function() { 

    $.ajax({ type: "POST", 
    url: "DemoWebService.asmx/GetFulName", 
    dataType: "xml", 
    data: "networkId=" + arg1, 
     processData: false, 
     error: function(XMLHttpRequest, textStatus, errorThrown) { ajaxError(XMLHttpRequest, textStatus, errorThrown); }, 
     success: function(xml) { ajaxFinish(xml); } 
    }); 

}); 
    function ajaxFinish(xml) { 
     // parse the object 
    } 

    function ajaxError(xmlObj, textStatus, errorThrown) { 
     // Comment this out for live environments, and put a friendly error message 
     alert("(Ajax error: " + txt + ")"); 
     alert(request.responseText); 
    } 

//]]> 
</script> 


[WebMethod] 
public string GetFulName(string networkId) 
{ 
    return networkId + "Full Name"; 
} 

Répondre

1

Vous pouvez définir une variable javascript dans votre balisage ASPX avec le nom d'utilisateur comme ceci:

<script type="text/javascript"> 
    var username = "<%= User.Identity.Name %>"; 
</script> 

Voir http://msdn.microsoft.com/en-us/library/system.security.principal.iidentity_members.aspx pour plus d'informations sur User.Identity.Name.

+0

Merci pour votre réponse. Erreur Le nom 'utilisateur' n'existe pas dans le courant Je reçois l'erreur ci-dessus. – Picflight

+0

L'utilisateur est une propriété de System.Web.UI.Page. Avez-vous essayé cela dans une page .aspx? – jrummell

1

Depuis la variable serveur AUTH_USER ne fonctionnait pas sur le client, je peux changer mon WebMthod pour obtenir le connecté nom d'utilisateur:

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