English 中文(简体)
When hosting an ADO.NET DataService<T> in a WCF WebServiceHost how does one get access to the credentials supplied in the request?
原标题:

If I have a type like:

public class Context
{
    public Context()
    {
    }
    public IQueryable<Record> Records
    {
        get
        {
            if (user == someone) //psuedocode
            {
               //return something
            }
            else
            {
               //return something else
            }
        }
    }
}

that I am hosting in a DataService like this:

WebServiceHost host = new WebServiceHost(typeof(DataService<Context>, "http://localhost:43334/");
WebHttpBinding binding = new WebHttpBinding();
ServiceEndpoint endpoint = host.AddServiceEndpoint(
           typeof(System.Data.Services.IRequestHandler), binding,
           "folder"); 
host.Open();

How does one gain access to the supplied credentials from the client-side request? I know there are options to deny access, but how do i actually get the supplied credentials to determine whom to deny and/or what Records could be accessible by a given user? I feel like this is either really easy and I am missing something, or that I am barking up the wrong tree.

最佳回答

To get access to the current logged-in user s credentials in your DataService ,You will need to :
a) Setup the Data Service to be able to access the current Web Http Context

using System.ServiceModel.Activation;
/// <summary>
/// Require that the WCF host setup access to the WebHttpContext of the currently executing request.
/// More details here : http://msdn.microsoft.com/en-us/library/aa702682.aspx
/// </summary>
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class ContextService : DataService<Context>

b) access the Current user s details via the HttpContext.Current.User property

if (System.Web.HttpContext.Current.User.Identity.Name == someone) { //return something
} else { //return something else
}

Some useful links
HttpContext.Current.User on MSDN
Sharing state between ASP.NET and WCF

问题回答

暂无回答




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Remotely authenticating client Windows user on demand

Suppose I am writing a server for a particular network protocol. If I know that the client is running on a Windows machine, is it possible for my server to authenticate the Windows user that owns the ...

Role/Permission based forms authorizing/authentication?

While looking into forms authorizing/authentication, I found that it is possible to do role based authorizing by adding an array of roles to a FormsAuthenticationTicket. That way I can write User....

热门标签