Dears,
I've designed a OData endpoint, following this https://learn.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint
I'm done with it but I have a constraint: for all API features, the header must contain an Authorization key. This is done everywhere BUT in the service document. The service document is what you got when looking at URL https://SITE/api/v1:
{"#odata.context":"http://SITE/api/v1/$metadata","value":[{"name":"things","kind":"EntitySet","url":"things"},{"name":"others","kind":"EntitySet","url":"others"}]}
Hence, do you know how is generated such document service? Is it possible to edit it? Or even, is there way to check header in such a document service?
Thanks in advance for your help,
Kind regards,
Solved by myself using following module:
using System;
using System.Linq;
using System.Web;
namespace Api.Modules
{
public class AuthorizationModule : IHttpModule
{
public void Dispose(){}
private void Context_Authorization(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
if (!IsAuthorized(application.Context))
{
application.Context.Response.Status = "403 Forbidden";
application.Context.Response.StatusCode = 403;
application.CompleteRequest();
}
}
public bool IsAuthorized(HttpContext context)
{
if (!HttpContext.Current.Request.Headers.AllKeys.Contains("Authorization"))
return false;
string authHeader = HttpContext.Current.Request.Headers["Authorization"];
if (!authHeader.Equals(Properties.Resources.Authorization_Key))
{
return false;
}
return true;
}
public void Init(HttpApplication context)
{
context.AuthenticateRequest += new EventHandler(Context_Authorization);
}
}
}
If it helps...
Kind regards,
Related
I am working on a asp.net mvc5 project and I want to implement chatroom with signalR So I got Microsoft.Aspnet.SignalR from nuget and I used a SignalR Hub class for hub and now i want to override OnDisconnected() method .but I get error
'ChatHub.OnDisconnected()': no suitable method found to override
I dont know how to solve this problem please help me
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using System.Collections.Concurrent;
using System.Threading.Tasks;
namespace WebApplication3.Hubs
{
public class ChatHub : Hub
{
public void Hello()
{
Clients.All.hello();
}
static ConcurrentDictionary<string, string> dic = new ConcurrentDictionary<string, string>();
public void Send(string name, string message)
{
Clients.All.broadcastMessage(name, message);
}
public void SendToSpecific(string name, string message, string to)
{
// Call the broadcastMessage method to update clients.
Clients.Caller.broadcastMessage(name, message);
Clients.Client(dic[to]).broadcastMessage(name, message);
}
public void Notify(string name, string id)
{
if (dic.ContainsKey(name))
{
Clients.Caller.differentName();
}
else
{
dic.TryAdd(name, id);
foreach (KeyValuePair<String, String> entry in dic)
{
Clients.Caller.online(entry.Key);
}
Clients.Others.enters(name);
}
}
public override Task OnDisconnected()
{
var name = dic.FirstOrDefault(x => x.Value == Context.ConnectionId.ToString());
string s;
dic.TryRemove(name.Key, out s);
return Clients.All.disconnected(name.Key);
}
}
}
For SignalR 2.1.0+, you need to use OnDisconected(bool stopCalled).
// Microsoft.AspNet.SignalR.Hub
// Summary:
// Called when a connection disconnects from this hub gracefully or due to a timeout.
//
// Parameters:
// stopCalled:
// true, if stop was called on the client closing the connection gracefully; false,
// if the connection has been lost for longer than the Microsoft.AspNet.SignalR.Configuration.IConfigurationManager.DisconnectTimeout.
// Timeouts can be caused by clients reconnecting to another SignalR server in scaleout.
//
// Returns:
// A System.Threading.Tasks.Task
public virtual Task OnDisconnected(bool stopCalled);
What is the difference between IOrganizationService and OrganizationServiceProxy in Dynamics CRM?
Is it related to accessing services in Crm context and outside Crm Context?
Simplest answer is IOrganizationService is an interface whereas OrganizationServiceProxy is a class which implements the IOrganizationService interface. This means any properties/methods on IOrganizationService are by default also available via OrganizationServiceProxy.
If running in the context of a plugin or custom workflow activity it will give you access to an IOrganizationService that you can use to interrogate CRM.
If you are writing something external, such as a windows service or standalone application, then you generally use the class OrganizationServiceProxy to set up a connection to the CRM web service. You can obviously assign this to an IOrganizationService later (e.g. dependency injection / for unit testing purposes). Or if you prefer there is no reason why you can continue to use the OrganizationServiceProxy.
The IOrganisationService is used within plugins and custom workflow activities and derived from the execution context.
While the OrganisationServiceProxy is mainly used for code running outside the Dynamics CRM application.
If you're using the sdk assemblies (specifically microsoft.xrm.sdk.dll), then you'll be using an implementation of IOrganizationService, and the call time will be identical. The main purpose of OrganizationServiceProxy is to provide options for establishing a connection to CRM in code that runs outside of CRM .The OrganizationServiceProxy class implements the IOrganizationService and provides an authenticated WCF channel to the organization service
OrganizationService is a higher level class that provides richer client side functionality and actually uses OrganizationServiceProxy inside it.
The Microsoft.Xrm.Client assembly that holds this higher level API cannot be used in Plugins etc but is intended for rich clients and ASP.NET.
It's worth noting that the Microsoft.Xrm.Client assembly has been removed from the CRM2016 SDK. For 2016 projects you might consider using the XRM Toolking assemblies.
See msdn.microsoft.com/.../dn689057.aspx
It has similar functionality that you mention around the connection manager -msdn.microsoft.com/.../mt608573.aspx
OrganizationServiceProxy is implementation of IOrganizationService. It is similar to case as List is implementation of interface IList. If to speak about why on Earth Microsoft provides both interface and implementation besides testing, I can propose one interesting case which happened in my life. I was in need of re-reading information from dynamics crm. In my case OrganizationServiceProxy got expired faster, then information was received from CRM. In order to fix it I've created following facade:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace OrganizationService
{
public delegate IOrganizationService CreateOrganizationServiceFunc(string organizationServiceUrl, string userName, string password, TimeSpan? timeout = null, bool useSSL = false);
public class OrganizationServiceFacade : IOrganizationService
{
private IOrganizationService _serviceInternal { get; set; }
private CreateOrganizationServiceFunc _creator;
Func<IOrganizationService> _orgServiceFactory;
public OrganizationServiceFacade(Func<IOrganizationService> orgServiceFactory)
{
_orgServiceFactory = orgServiceFactory;
CreateService();
}
private void CreateService()
{
_serviceInternal = _orgServiceFactory();
}
public void Associate(string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection relatedEntities)
{
try
{
_serviceInternal.Associate(entityName, entityId, relationship, relatedEntities);
}
catch (System.ServiceModel.Security.MessageSecurityException mex)
{
CreateService();
_serviceInternal.Associate(entityName, entityId, relationship, relatedEntities);
}
}
public Guid Create(Entity entity)
{
Guid result;
try
{
result = _serviceInternal.Create(entity);
}
catch (System.ServiceModel.Security.MessageSecurityException mex)
{
CreateService();
result = _serviceInternal.Create(entity);
}
return result;
}
public void Delete(string entityName, Guid id)
{
try
{
_serviceInternal.Delete(entityName, id);
}
catch (System.ServiceModel.Security.MessageSecurityException mex)
{
CreateService();
_serviceInternal.Delete(entityName, id);
}
}
public void Disassociate(string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection relatedEntities)
{
try
{
_serviceInternal.Disassociate(entityName, entityId, relationship, relatedEntities);
}
catch (System.ServiceModel.Security.MessageSecurityException mex)
{
CreateService();
_serviceInternal.Disassociate(entityName, entityId, relationship, relatedEntities);
}
}
public OrganizationResponse Execute(OrganizationRequest request)
{
try
{
return _serviceInternal.Execute(request);
}
catch (System.ServiceModel.Security.MessageSecurityException mex)
{
CreateService();
return _serviceInternal.Execute(request);
}
}
public Entity Retrieve(string entityName, Guid id, ColumnSet columnSet)
{
try
{
return _serviceInternal.Retrieve(entityName, id, columnSet);
}
catch (System.ServiceModel.Security.MessageSecurityException mex)
{
CreateService();
return _serviceInternal.Retrieve(entityName, id, columnSet);
}
}
public EntityCollection RetrieveMultiple(QueryBase query)
{
try
{
return _serviceInternal.RetrieveMultiple(query);
}
catch (System.ServiceModel.Security.MessageSecurityException mex)
{
CreateService();
return _serviceInternal.RetrieveMultiple(query);
}
}
public void Update(Entity entity)
{
try
{
_serviceInternal.Update(entity);
}
catch (System.ServiceModel.Security.MessageSecurityException mex)
{
CreateService();
_serviceInternal.Update(entity);
}
}
}
}
and then I've got simple reconnect mechanism:
Trace.TraceInformation("Creation of CRM connection");
for (var maxTry = 0; maxTry < reconectionTries; maxTry++)
{
try
{
var service = new OrganizationServiceFacade(() =>
XrmServiceCreator.CreateOrganizationService("organizationServiceUrl",
"username",
"userpassword",
DefaultTimeout));
var response = (WhoAmIResponse) service.Execute(new WhoAmIRequest());
if (response.Results.Count == 0)
{
throw new InvalidDataException($"CRM returned no data in response. Number of retries is: {maxTry}, user name: {ConfigSettings.Default.RunAsUser}, Default timeout = {DefaultTimeout}");
}
return service;
}
catch (Exception e)
{
Trace.TraceError("Exception: {0}", e);
}
}
method XrmServiceCreator.CreateOrganizationService returns instance of IOrganizationService, and behind curtains it creates instance of OrganizationServiceProxy
This is quite straight forward(ish) to do is the event is 'real' as in now created by DynamicProxy, but I can't work anything out for a mocked event.
The best way to explain what I'm trying to achieve is with code, please see the comment lines in the test method:
using System;
using Moq;
using NUnit.Framework;
namespace MOQTest
{
[TestFixture]
public class EventsMoqTest
{
[Test]
public void DetachTest()
{
var hasEventMock = new Mock<IHasEvent>();
using (var observer = new Observer(hasEventMock.Object))
{
//Assert that hasEventMock.Object has handler attached
}
//Assert that hasEventMock.Object DOES NOT have handler attached
}
}
public interface IHasEvent
{
event EventHandler AnEvent;
}
public class Observer : IDisposable
{
private readonly IHasEvent _hasEvent;
private readonly EventHandler _hasEventOnAnEvent;
public Observer(IHasEvent hasEvent)
{
_hasEvent = hasEvent;
_hasEventOnAnEvent = _hasEvent_AnEvent;
_hasEvent.AnEvent += _hasEventOnAnEvent;
}
void _hasEvent_AnEvent(object sender, EventArgs e)
{}
public void Dispose()
{
_hasEvent.AnEvent -= _hasEventOnAnEvent;
}
}
}
Unfortunately, you can't. This isn't really a moq issue, but the way the C# event keyword works with delegates. See this SO answer for more information.
My project has a need for realtime user interaction and I think SignalR will solve my need. I'm technically on a SharePoint 2007 project, although I'm exclusively in application pages and thus barely use SharePoint at all. Regardless, I'm stuck in a 2.0 framework app pool in IIS.
My first approach was to try to create a 4.0 application as a sub-site. Unfortunately, that failed miserably. That approach works in a non-SharePoint world, but it appears that SharePoint has hijacked too much of the request pipeline for this approach to work for me.
So now I'm going down the path of creating a separate IIS Site that's 4.0 and using IIS rewrite rules to fake my app into thinking a particular subdirectory (/realtime/) is local and not a separate site so that I don't have to deal with cross domain request issues. The problem is I can't get IIS rewrite rules to rewrite to another http host (e.g. http://www.mySharepoint.com/_layouts/MySite/realtime/Hello.aspx to http://realtime.mySharePoint.com/Hello.aspx).
Any help with approach #1 or approach #2 or any alternative ideas would be greatly appreciated.
Here is what I did... Web App with signalR .net4.0, then your SharePoint Web App .net 2.
Add this to the global.asax in your Signalr project
RouteTable.Routes.MapHttpHandlerRoute("spproxy","spproxy/{*operation}", new SharePointRProxyHandler());
If you want to raise an event from SharePoint you can do a http POST to this new route URL for example
http://localhost:38262/spproxy
It will pass any posted data onto the httphandler below, that will then broadcast it to your clients.
Here is the code for MapHttpHandlerRoute
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace System.Web.Routing
{
public class HttpHandlerRoute : IRouteHandler
{
private String _virtualPath = null;
private IHttpHandler _handler = null;
public HttpHandlerRoute(String virtualPath)
{
_virtualPath = virtualPath;
}
public HttpHandlerRoute(IHttpHandler handler)
{
_handler = handler;
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
IHttpHandler result;
if (_handler == null)
{
result = (IHttpHandler)System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(_virtualPath, typeof(IHttpHandler));
}
else
{
result = _handler;
}
return result;
}
}
public static class RoutingExtensions
{
public static void MapHttpHandlerRoute(this RouteCollection routes, string routeName, string routeUrl, string physicalFile, RouteValueDictionary defaults = null, RouteValueDictionary constraints = null)
{
var route = new Route(routeUrl, defaults, constraints, new HttpHandlerRoute(physicalFile));
RouteTable.Routes.Add(routeName, route);
}
public static void MapHttpHandlerRoute(this RouteCollection routes, string routeName, string routeUrl, IHttpHandler handler, RouteValueDictionary defaults = null, RouteValueDictionary constraints = null)
{
var route = new Route(routeUrl, defaults, constraints, new HttpHandlerRoute(handler));
RouteTable.Routes.Add(routeName, route);
}
}
}
Or you could just post directly to a httphandler and get the handler to do a connection.Broadcast
namespace SharePointRProxy
{
/// <summary>
/// Summary description for SharePointRProxyHandler
/// </summary>
public class SharePointRProxyHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
IConnectionManager connectonManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
IConnection connection = connectonManager.GetConnection<MyConnection>();
object payload = null; //Add payload here 'context.Request.Params["data"] ?'
JavaScriptSerializer jss = new JavaScriptSerializer();
var payloadJSON = jss.Serialize(payload);
connection.Broadcast(payloadJSON);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
You could also use either an event handler calling a .net 4.0 web service or an http handler to grab requests from SharePoint and pass them over to a .net 4.0 application running your signalr code.
You can see an example of using an http handler here: http://spmatt.wordpress.com/2012/04/12/harnessing-signalr-in-sharepoint/
Does anyone knows how i can resize the PhotoshopImage instance?
I don't us the UIImageView because i need to load a lot of images and the PhotoshopImage class handles it better.
Got a real shaky start for you! Photoshop has documentation on using javascript vbscript with photoshop dll's: http://www.adobe.com/devnet/photoshop/scripting.html. These same methods are exposed via COM to C# and I wonder if they're available to Objective-C (RedGate and the vs object browser can help if you dabble with it). Don't cringe at the C# code! The point is photoshop exposes dlls which can be worked with. C# ASP.NET exposes photoshop .dll's via COM. I'm new to objective-c and not a vet at C#! I got this code to work on my windows machine in C#. This code cranks up a web page and fires up my version of photoshop cs3 and goes thru my directory of files and creates an "Adobe image gallery". Good luck to you and post back what you find in objective-c...I think objective-c can run native C and I've seen some documentation of working with photoshop in native C...Shoot some code back either way...I'm a semi newbie so if this wasn't what you meant by Photoshop Image I apologize!
CDUB
PS these are all photoshop methods being exposed, nothing I made up...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GoogleTalkAPILib;
using ps = Photoshop;
using Photoshop;
namespace photoshop
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
Object ob= null;
//works!!!!!!
// co.Application.MakePDFPresentation(oaa,
"C:\Users\Photoshoptryrescl",ob);
//you can also use c# to run a javascript
// co.DoJavaScript("hey.js",e,d );
co.MakePhotoGallery(oab, "C:\\photoshopdump", ob);
}
catch (Exception ex)
{ Trace.Write(ex.Message.ToString()); }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ps = Photoshop;
using Photoshop;
using Microsoft.Win32.SafeHandles;
using Microsoft.Win32;
using Microsoft;
namespace photoshop
{
public delegate void addBlur();
public class Class1 : ApplicationClass, ArtLayer, Document
{
public Class1()
{ }
public void addBlur()
{ }
public void addBlur1(string sa)
{ }
#region ArtLayer Members
public void AdjustBrightnessContrast(int Brightness, int Contrast)
{
throw new NotImplementedException();
}
public void AdjustColorBalance(object Shadows, object Midtones, object Highlights, object PreserveLuminosity)
{
throw new NotImplementedException();
}
public void AdjustCurves(object CurveShape)
{
throw new NotImplementedException();
}
public void AdjustLevels(int InputRangeStart, int InputRangeEnd, double InputRangeGamma, int OutputRangeStart, int OutputRangeEnd)
{
throw new NotImplementedException();
}
public bool AllLocked
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public void ApplyAddNoise(double Amount, PsNoiseDistribution Distribution, bool Monochromatic)
{
throw new NotImplementedException();
}
public void ApplyAverage()
{
throw new NotImplementedException();
}
public void ApplyBlur()
{
// throw new NotImplementedException();
}
public void ApplyBlurMore()
{
throw new NotImplementedException();
}
//etc... these interfaces expose a ton of methods which can be explicity implemented
//this isn't them all