WCF Service fails to send mail through MailDefinition - c#

// stuff......
return SendCreationMail(Membership.GetUser((Guid)request.UserEntityId), request, new Control());
}
private const string TemplateRoot = "~/app_shared/templates/mail/";
private const string ServerRoot = TemplateRoot + "server/";
public static bool SendCreationMail(MembershipUser user, IServerAccountRequest request, Control owner)
{
var definition = new MailDefinition { BodyFileName = string.Concat(ServerRoot, "creation.htm"), IsBodyHtml = true };
var subject = "New {0} account created!".FormatWith(request.ServerApplicationContract.Id);
var data = ExtendedData(DefaultData, subject, user);
data.Add("<%ServerApplication%>", request.ServerApplicationContract.Id);
data.Add("<%ServerApplicationName%>", request.ServerApplicationContract.ApplicationName);
data.Add("<%AccountUsername%>", request.AccountUsername);
data.Add("<%ServerInfo%>", "/server/{0}/info".FormatWith(request.ServerApplicationContract.Id.ToLower()));
return definition.CreateMailMessage(user.Email, data, owner).Send(subject, ApplicationConfiguration.MailSenderDisplayName);
}
I get:
Value cannot be null. Parameter name: basepath
at System.Web.Util.UrlPath.Combine(String appPath, String basepath,
String relative)
at
System.Web.UI.WebControls.MailDefinition.CreateMailMessage(String
recipients, IDictionary replacements, Control owner)
at
Damnation.Website.Main.Common.MailTemplating.Server.SendCreationMail(MembershipUser
user, IServerAccountRequest request, Control owner)
at Damnation.Website.Main.Common.MailTemplating.Server.SendCreationMail(IServerAccountRequest
request)
at Damnation.Website.Main.Business.Model.ServerAccountRequest.UpdateRequestsAfterServerProcessing(IEnumerable`1
results)
Thing is I don't have an actual Control to pass to it, I'd like to know how to setup a control that avoids this senseless exception. I'm using a relative path... so this makes no sense.
The application that has the service is under ASP.NET WebForms .NET 4. The consumer application is a console application also under .NET 4

I had the same problem, and I learned that this is happening because it cannot find the path of the control when trying to execute your definition.CreateMailMessage. You want to create a blank web user control. Maybe this is not the most elegant solution but it does the work.
This is what I did:
1) Add a Web User Control file in your project, for example Test.ascx.
2) In your .svc file add the following code:
Page page = new Page();
Test test = (Test)page.LoadControl("Test.ascx");
3) Update your definition.CreateMailMessage line to:
return definition.CreateMailMessage(user.Email, data, test).Send(subject, ApplicationConfiguration.MailSenderDisplayName);
You will no longer get the basepath null exception.

I had the same problem but it was because I was on a project .NET console application, when I passed the code to ASP.NET not I run well without creating Page and Test.

I had the same error but the problem for me was that I was using relative paths for my BodyFileName and EmbeddedObjects. Changing them to absolute paths fixed this error for me.

Related

Determine if a given url (from a string) is from my domain or not

I'm trying to check from c# code if a given url is from my domain or not, in order to add the "nofollow" and "target _Blank" attributes for external links.
When i talk about external links i refer to any link outside my domain.
By default it does not have that attributes. I tried a lot of stuff, basically this is the part i need to fix:
public void PrepareLink(HtmlTag tag)
{
string url = tag.attributes["href"];
if (PrepareLink != null)
{
if (it is from an external site???)
{
tag.attributes["rel"] = "nofollow";
tag.attributes["target"] = "_blank";
}
}
Edit:
things i've tried:
string dominioLink = new Uri(url).Host.ToLower();
if (!dominioLink.Contains(myDomainURL))
{
tag.attributes["rel"] = "nofollow";
tag.attributes["target"] = "_blank";
}
Which has the issue that dont take in mind subdomains
i.e. if a link created is http://www.mydomain.com.anotherfakedomain.com, it will return true and work well.
I've looked in every Uri property but didn't seem to contains the base domain.
I'm currently using .NET Core 2.0.
thankS! please if you need any other data just let me know.
You can use the Uri.Host property to obtain the domain from a URL string, then compare it to your own. I suggest using a case-insensitive match.
var url = tag.attributes["href"];
var uri = new Uri(url);
var match = uri.Host.Equals(myDomain, StringComparison.InvariantCultureIgnoreCase)

Cache player with GameSparks and Unity

I am new to gamesparks, but so far I have set up a login/register function, which works like it should, but... How do I ensure that the user don't have to login next time he or she opens the app?
I found this in which I read that I can just run this:
GameSparkssApi.isAuthenticated().
First off all, in all other tutorials it states that it should be: GameSparks.Api.xxxx. Even when trying this I do not find isAuthenticated() anywhere.
GameSparks.Api.isAuthenticated();
I am hoping that someone can cast some light on this.
You can use Device Authentication for this purpose. This method of auth sets an access token for the device you are on. This token is stored and the client and gotten in the request. these requests are structured like so:
new GameSparks.Api.Requests.DeviceAuthenticationRequest()
.SetDeviceId(deviceId)
.SetDeviceModel(deviceModel)
.SetDeviceName(deviceName)
.SetDeviceOS(deviceOS)
.SetDeviceType(deviceType)
.SetDisplayName(displayName)
.SetOperatingSystem(operatingSystem)
.SetSegments(segments)
.Send((response) => {
string authToken = response.AuthToken;
string displayName = response.DisplayName;
bool? newPlayer = response.NewPlayer;
GSData scriptData = response.ScriptData;
var switchSummary = response.SwitchSummary;
string userId = response.UserId;
});
You can find more on this method in our documentation: https://api.gamesparks.net/#deviceauthenticationrequest
Regards Patrick, GameSparks.

Redirecting Virtual User to Content Editor Programatically

I am trying to create a Virtual user with and redirect to content editor as below.
string userId = string.Format("{0}\\{1}", "sitecore", "testadmin");
var scUser = AuthenticationManager.BuildVirtualUser(userId, true);
scUser.RuntimeSettings.IsAdministrator = true;
scUser.RuntimeSettings.AddedRoles.Add(#"sitecore\Sitecore Client Authoring");
AuthenticationManager.Login(scUser);
string url = "/sitecore/shell/sitecore/content/Applications/Content Editor.aspx?id=%7b110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9%7d&la=en&fo=%7b110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9%7d";
url = string.IsNullOrEmpty(url) ? "/" : url;
HttpContext.Current.Response.Redirect(url, false);
But it always redirects the user to sitecore/login page.
Any idea what is the issue here?
Interesting. I'm not entirely sure that approach is a supported scenario. However, the Content Editor runs off the "shell" website, possibly that is your issue.
Try putting this code around your entire code block.
using(new SiteContextSwitcher("shell")) {
}
You need to change:
AuthenticationManager.Login(scUser);
to
AuthenticationManager.LoginVirtualUser(scUser);

How to get the url prefixes asp.net MVC

I have a ASP.NET MVC application which will work on a IIS7.
I don't know the final URL yet, and that's the problem.
I want to get the URL-Parts between the Top-level-domain and my controller.
For examople: http://www.mydomain.com/myApplication/MyController/ControllerMethodshould return /myApplication/MyController/
This should also be possible, if the application is called via the standard method, for example http://www.mydomain.com/myApplication.
The Problem is that with my method it works perfectly if the full controller- and methodname is in the url, but as soon as there is only the controller name and the route takes the default index-method or there is no controller/method and the route takes the default controller/method, it will fail because my code puts wrong output.
I thought about hardcoding the controller-name and make a if-then-else orgy, but this doesn't seem very professional...
Maybe anyone of you has got an Idea.
Here's my function:
String segments = Request.Url.Segments;
System.Text.StringBuilder builder = new System.Text.StringBuilder();
String lastSegment = "";
int i= 0;
do
{
builder.Append(segments[i]);
lastSegment = segments[i++];
} while(!lastSegment.Equals("Home") && !lastSegment.Equals("Home/") && i < segments.Length);
return builder.toString();
Use the Url class to build the Urls for you http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper(v=vs.118).aspx

Lists.AddAttachment throws object reference error

I'm trying to add an attachment to a list item I just created on a sharepoint server using CAML. The code below is simplified (for example the path is normally a variable and the 2nd field (the item id, here 16847) is normally the id I get back from my insert statement in CAML).
This is my code:
String desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
String savePath = desktopPath + #"\" + "tutorials.txt";
byte[] data = GetData(savePath);
lists.AddAttachment("Tasks", "16847", "tutorials.txt", data);
I'm getting this error:
Unable to update the security according your changes. The following exception occurred during ItemUpdating: Object reference not set to an instance of an object.
See Event Viewer for more information.0x81020089
getdata is a method which converts the file on my desktop to a byte[]. data is not empty and it looks okay.
Other than that Tasks is the List needed and 16847 is the task id in which to add the attachment.
Most of the info I can find is about a different error: the index out of range exception (like on the msdn page: http://msdn.microsoft.com/en-us/library/lists.lists.addattachment(v=office.12).aspx).
I've also tried a http put (unauthorized access, obviously) and the copy service (I can post this code if we can't find the solution to the simpler way above).
Can anyone tell me what is wrong?
EDIT 1:
private dcp.Lists lists = new dcp.Lists();
lists.Credentials = System.Net.CredentialCache.DefaultCredentials;
lists.Url = Values.SERVERADDRESS + "/_vti_bin/lists.asmx";
This initializes the connection to our web service. It works perfectly fine for updates, inserts...
GetData code:
private byte[] GetData(String savePath)
{
byte[] contents;
using (FileStream fStream = File.OpenRead(savePath))
{
contents = new byte[fStream.Length];
sFileName = fStream.Name;
fStream.Read(contents, 0, Convert.ToInt32(fStream.Length));
}
return contents;
}
EDIT 2:
Note that the following does work (I get a correct list of existing attachments from the task):
XmlNode ndAttach = lists.GetAttachmentCollection("Tasks", "16847");
MessageBox.Show(ndAttach.OuterXml);
And the following doesn't (same error as for the AddAttachment method):
lists.DeleteAttachment("Tasks", "16847", ndAttach.ChildNodes[0].InnerText);
Whereas I'm quite certain this should work since it does exactly the same as the example code on msdn: http://msdn.microsoft.com/en-us/library/lists.lists.deleteattachment(v=office.12).aspx
The code above is fine, the error occured on our server. When adding an attachment, no contenttype is given in the xml. This was programmed to throw an error because we've always used this method to update and add new items. So we removed the contenttype out of the underlying code and it works like a charm.

Categories