2014-10-17 32 views
0

我有WCF休息服務,我需要得到這個服務中的用戶名 如果我用CONSOL應用程序進行查詢,我可以得到名稱(OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name),但如果我使用ajax我的Operationaltext我的 。 Current.ServiceSecurityContext = NULL。我如何從ajax查詢獲取用戶名?我如何獲取用戶名?

合同

public interface IService1 
{ 
    [OperationContract()] 
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] 
    List<Data.Test> GetTestData(); 

    [OperationContract] 
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] 
    void AddTestData(Data.Test value); 
} 

服務

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class Service1 : IService1 
{ 

    private static List<Data.Test> _data = new List<Data.Test>() { new Data.Test() { ID = 0, Name = "a" }, new Data.Test() { ID = 1, Name = "B" } }; 
    public List<Data.Test> GetTestData() 
    { 

     _data.Add(new Test { ID = 3, Name = OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name}); 
     return _data; 
    } 

    public void AddTestData(Data.Test value) 
    { 
     _data.Add(value); 
    } 

AJAX查詢

function GETData() { 
    $.ajax({ 
     type: 'Post', 
     url: "http://localhost:50711/Service1.svc/GetTestData", 
     dataType: 'json', 
     success: function (data) { 
      ShowData(data); 
     }, 
     error: function (e) { 
      console.log(e.message); 
     } 
    }); 
}; 
+0

更改要獲取的方法名稱。 – vam 2014-10-17 12:08:07

+0

Get,not't work – Voucik 2014-10-17 13:37:02

+0

在你的ajax函數中改變類型爲「Type」,「T」應該是upppercase。並且改變類型名稱。 – vam 2014-10-27 06:08:12

回答

0

要做到這一點,最好的方法是創建一個會話。你很容易就可以打電話給誰做了什麼。使用會話