FBA authorization in custom WCF SharePoint - wcf

I have soap WCF SharePoint:
[ServiceContract]
public interface IService
{
[OperationContract]
string Test();
}
[BasicHttpBindingServiceMetadataExchangeEndpoint]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class Service : IService
{
public string Test()
{
WindowsIdentity identity = ServiceSecurityContext.Current.WindowsIdentity;
ClaimsIdentity claimsIdentity = new ClaimsIdentity(identity);
return SPContext.Current.Web.Title;
}
}
This service mapped on ISAPI.On SharePoint use FBA. And How connect to WCF that impersonate/authenticate FBA user?

When enable windows and fba authentication order to use windows authentciation need add header "X-FORMS_BASED_AUTH_ACCEPTED" with value "f": I use restsharp:
request.AddHeader("X-FORMS_BASED_AUTH_ACCEPTED", "f")

Related

Get current user id in startup (asp.net core 5)

How I can get current user id in Configure Services method.
I need to send user Id when inject some service.
or it is not possible ?
I need to send user Id when inject some service. or it is not possible ?
HttpContext is only valid during a request. The ConfigureServices method in Startup is not a web call and, as such, does not have a HttpContext. So the User information also cannot be got. You need register the IHttpContextAccessor and DI it by constructor in your services. Then you could get HttpContext.User infomation successfully.
Register the IHttpContextAccessor:
services.AddScoped<IHttpContextAccessor,HttpContextAccessor>();
DI in the service:
public interface IService
{
string GetUserId();
}
public class Service : IService
{
private readonly IHttpContextAccessor _httpContextAccessor;
public Service(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public string GetUserId()
{
var id = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;
return id;
}
}
Register the service:
services.AddScoped<IHttpContextAccessor,HttpContextAccessor>();
services.AddScoped<IService, Service>(); //add this...
More explanations you could refer to here.

WCF Service - HttpContext.Current.User not logged out using FormsAuthentication on Silverlight

I have a basic Silverlight Business Application which also hosts a WCF service. The service is secured to require Administrators role.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class Service1 : IService1
{
public Service1()
{
Thread.CurrentPrincipal = HttpContext.Current.User;
}
[PrincipalPermission(SecurityAction.Demand, Role = "Administrators")]
public void DoWork()
{
}
}
Oddly, after logging out of the Silverlight Application, I was still able to replay the service call to DoWork() successfully using Fiddler.
Setting a breakpoint on Thread.CurrentPrincipal = HttpContext.Current.User; showed that the HttpContext.Current.User was still holding the previously authenticated users identity.
What is going on here?

Contract requires Duplex, but Binding 'BasicHttpBinding' doesn't support it or isn't configured properly to support it

my project is silverlight and use Ria service.
I want to create a WCF Service,
this is my code
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class MessageDuplex : IServiceSide
{
public void Attach(string s)
{
}
}
[ServiceContract]
interface IClientSide
{
[OperationContract(IsOneWay = true)]
void SendMessage(string s);
}
[ServiceContract(CallbackContract = typeof(IClientSide))]
interface IServiceSide
{
[OperationContract(IsOneWay = true)]
void Attach(string s);
}
When i defined IServiceSide without CallbackContract this is add to silverlight project correctly, but when i use CallbackContract this error raise when add Service Referense :
"Contract requires Duplex, but Binding 'BasicHttpBinding' doesn't support it or isn't configured properly to support it."
BasicHttpBinding doesn't support duplex services. You must use PollingDuplexHttpBinding.

SOAP Web Services Attributes to verify authentication token

I would like to create some sort of authentication attribute and attach it to various OperationContracts. Inside this attribute, it would check for an authentication token and make sure its still valid before the OperationContract is run.
What's the best way to implement this on the .net platform? Does wcf have any special attributes that already do this type of functionality? What I'm picturing is something similar to the attributes you can attach to MVC controllers that will perform operations before actions are run.
In case it's relevant, I am using WCF to create SOAP web services that will be consumed by clients on various platforms that support SOAP.. not just WCF clients
Here's some code to clarify what I'm trying to do:
interface:
[ServiceContract]
public interface IService
{
[OperationContract]
string ValidateUser(string username, string password);
[OperationContract]
string GetDataAndAuthInCode(string authtoken);
[MyAuthorizationAttribute]
[OperationContract]
string GetDataAndAuthWithAttribute(string authtoken);
}
implementation:
public class Service : IService
{
public string ValidateUser(string username, string password)
{
if (!Membership.ValidateUser(username, password))
throw new Exception("invalid user...");
else
return GenerateAuthToken(username);
}
public string GetDataAndAuthInCode(string authtoken)
{
if (!IsAuthTokenValid(authtoken))
throw new Exception("Auth token invalid expired");
else
return GetData();
}
public string GetDataAndAuthWithAttribute(string authtoken)
{
return GetData();
}
}
Looks like this is what I'm looking for.. "Custom Behaviors":
http://msdn.microsoft.com/en-us/magazine/cc163302.aspx#S7

How to call a service operation at a REST style WCF endpoint uri?

is it possible to call a service operation at a wcf endpoint uri with a self hosted service?
I want to call some default service operation when the client enters the endpoint uri of the service.
In the following sample these uris correctly call the declared operations (SayHello, SayHi):
- http://localhost:4711/clerk/hello
- http://localhost:4711/clerk/hi
But the uri
- http://localhost:4711/clerk
does not call the declared SayWelcome operation. Instead it leads to the well known 'Metadata publishing disabled' page. Enabling mex does not help, in this case the mex page is shown at the endpoint uri.
private void StartSampleServiceHost()
{
ServiceHost serviceHost = new ServiceHost(typeof(Clerk), new Uri( "http://localhost:4711/clerk/"));
ServiceEndpoint endpoint = serviceHost.AddServiceEndpoint(typeof(IClerk), new WebHttpBinding(), "");
endpoint.Behaviors.Add(new WebHttpBehavior());
serviceHost.Open();
}
[ServiceContract]
public interface IClerk
{
[OperationContract, WebGet(UriTemplate = "")]
Stream SayWelcome();
[OperationContract, WebGet(UriTemplate = "/hello/")]
Stream SayHello();
[OperationContract, WebGet(UriTemplate = "/hi/")]
Stream SayHi();
}
public class Clerk : IClerk
{
public Stream SayWelcome() { return Say("welcome"); }
public Stream SayHello() { return Say("hello"); }
public Stream SayHi() { return Say("hi"); }
private Stream Say(string what)
{
string page = #"<html><body>" + what + "</body></html>";
return new MemoryStream(Encoding.UTF8.GetBytes(page));
}
}
Is there any way to disable the mex handling and to enable a declared operation instead?
Thanks in advance, Dieter
Did you try?
[OperationContract, WebGet(UriTemplate = "/")]
Stream SayWelcome();
UPDATE:
Not sure why it is not working for you, I have a self hosted WCF service with the following service contract:
[ServiceContract]
public interface IDiscoveryService {
[OperationContract]
[WebGet(BodyStyle=WebMessageBodyStyle.Bare, UriTemplate="")]
Stream GetDatasets();
The only difference I can see is that I use WebServiceHost instead of ServiceHost.