我怎么能得到用户名?

时间:2014-10-17 11:51:28

标签: ajax wcf

我有WCF休息服务,我需要在此服务中获取用户名 如果我使用consol app进行查询,我可以获得名称(OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name),但如果我使用ajax我的 OperationContext.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);
        }
    });
};

1 个答案:

答案 0 :(得分:0)

最好的方法是创建一个会话。这对你来说很容易打电话给谁做了什么。使用会话