Mercurial > altnet-hispano
comparison PostWriterWebService blog/PostWriterWebService.asmx @ 98:4895116b8232
subida del web service para escribir posts en el blog del portal
author | jorge.rowies |
---|---|
date | Sat, 04 Jun 2011 14:10:21 -0300 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
89:24e9488ac152 | 98:4895116b8232 |
---|---|
1 <%@ WebService Language="C#" Class="PostWriterWebService" %> | |
2 | |
3 using System; | |
4 using System.Configuration; | |
5 using System.Globalization; | |
6 using System.Linq; | |
7 using System.Security.Cryptography; | |
8 using System.Web; | |
9 using System.Web.Services; | |
10 using System.Web.Services.Protocols; | |
11 using Telerik.Blogs; | |
12 | |
13 [WebService(Namespace = "http://tempuri.org/")] | |
14 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] | |
15 // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. | |
16 // [System.Web.Script.Services.ScriptService] | |
17 public class PostWriterWebService : System.Web.Services.WebService { | |
18 | |
19 [WebMethod] | |
20 public void WriteBlogPost(string securityToken, string blogName, string postTitle, string postHtmlContent, string postAuthor, bool setPublicationDate) | |
21 { | |
22 checkSecurityToken(securityToken); | |
23 writePost(blogName, postTitle, postHtmlContent, postAuthor, setPublicationDate); | |
24 } | |
25 | |
26 private void checkSecurityToken(string securityToken) | |
27 { | |
28 if (string.IsNullOrEmpty(securityToken)) throw new ArgumentNullException("securityToken"); | |
29 | |
30 var now = DateTime.UtcNow; | |
31 now = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0, 0); | |
32 | |
33 var masterKey = System.Configuration.ConfigurationManager.AppSettings["BlogWriterMasterKey"]; | |
34 if (string.IsNullOrEmpty(masterKey)) | |
35 throw new Exception("Master key not found"); | |
36 | |
37 var token1 = buildToken(masterKey, now.AddMinutes(-1)); | |
38 var token2 = buildToken(masterKey, now); | |
39 var token3 = buildToken(masterKey, now.AddMinutes(1)); | |
40 | |
41 if (!securityToken.Equals(token1) && !securityToken.Equals(token2) && !securityToken.Equals(token3)) | |
42 throw new Exception("Invalid security token"); | |
43 } | |
44 | |
45 private string buildToken(string key, DateTime time) | |
46 { | |
47 var aux = key + time.ToString(CultureInfo.InvariantCulture); | |
48 | |
49 var enc = System.Text.Encoding.ASCII.GetEncoder(); | |
50 | |
51 var data = new byte[aux.Length]; | |
52 enc.GetBytes(aux.ToCharArray(), 0, aux.Length, data, 0, true); | |
53 | |
54 var md5 = new MD5CryptoServiceProvider(); | |
55 var result = md5.ComputeHash(data); | |
56 | |
57 return BitConverter.ToString(result).Replace("-", "").ToLower(); | |
58 } | |
59 | |
60 private void writePost(string blogName, string postTitle, string postHtmlContent, string postAuthor, bool setPublicationDate) | |
61 { | |
62 if (string.IsNullOrEmpty(blogName)) throw new ArgumentNullException("blogName"); | |
63 if (string.IsNullOrEmpty(postTitle)) throw new ArgumentNullException("postTitle"); | |
64 if (string.IsNullOrEmpty(postHtmlContent)) throw new ArgumentNullException("postHtmlContent"); | |
65 if (string.IsNullOrEmpty(postAuthor)) throw new ArgumentNullException("postAuthor"); | |
66 | |
67 try | |
68 { | |
69 var manager = new BlogManager(); | |
70 | |
71 var blogs = manager.GetBlogs(); | |
72 var blog = (blogs.Cast<IBlog>()).SingleOrDefault(b => b.Name.Equals(blogName)); | |
73 | |
74 if (blog == null) | |
75 throw new Exception(string.Format("Blog '{0}' no encontrado", blogName)); | |
76 | |
77 var postContent = manager.Content.CreateContent("text/html"); | |
78 | |
79 postContent.ParentID = blog.ID; | |
80 postContent.Content = postHtmlContent; | |
81 postContent.SetMetaData("Author", postAuthor); | |
82 postContent.SetMetaData("Title", postTitle); | |
83 if (setPublicationDate) | |
84 postContent.SetMetaData("Publication_Date", DateTime.Now); | |
85 | |
86 manager.Content.SaveContent(postContent); | |
87 } | |
88 catch (Exception e) | |
89 { | |
90 throw new Exception("Ocurrio un error creando el post", e); | |
91 } | |
92 } | |
93 } |