我怎么能得到用户名?

时间:2022-01-06 02:59:43

I have WCF rest service and I need get user name within this service If I make query with consol app, I can get name (OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name), but if I use ajax my OperationContext.Current.ServiceSecurityContext=null. How can I get user name from ajax query?

我有WCF休息服务,我需要在此服务中获取用户名如果​​我使用consol应用程序进行查询,我可以获取名称(OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name),但如果我使用ajax我的OperationContext.Current.ServiceSecurityContext =空值。如何从ajax查询中获取用户名?

Contract

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);
}

Service

[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 query

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 个解决方案

#1


0  

The best way to do that is to create a session. That will be easy for you to call whoever did what. Use sessions

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

#1


0  

The best way to do that is to create a session. That will be easy for you to call whoever did what. Use sessions

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