|
Hello,
I have problem with insert timestamp to PDF.
On line Array.Copy(pk, 0, outc, 0, pk.Length); is error because outc.Length = 4096, but pk.Length = 7541. Metod DejCasoveRazitko work correctly.
P.S. Sorry, my English is bad
private const String ID_TIME_STAMP_TOKEN = "1.2.840.113549.1.9.16.2.14";
public void PodepisPdfCer(string SouborPDF, string OtiskCertifikatu)
{
var store = new System.Security.Cryptography.X509Certificates.X509Store(System.Security.Cryptography.X509Certificates.StoreName.My, System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser);
store.Open(System.Security.Cryptography.X509Certificates.OpenFlags.ReadOnly);
string thumbprint = OtiskCertifikatu.Replace(" ", "").ToUpperInvariant();
if (thumbprint[0] == 8206)
{
thumbprint = thumbprint.Substring(1);
}
var certs = store.Certificates.Find(System.Security.Cryptography.X509Certificates.X509FindType.FindByThumbprint, thumbprint, true);
store.Close();
if (certs.Count == 0)
{
MessageBox.Show("Nelze najít určený certifikát v Current user certificate store!", "Sign PDF", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
byte[] pdfData = File.ReadAllBytes(SouborPDF);
using (MemoryStream stream = new MemoryStream())
{
var reader = new PdfReader(pdfData);
var stp = PdfStamper.CreateSignature(reader, stream, '\0');
var sap = stp.SignatureAppearance;
stp.SetEncryption(null,
Guid.NewGuid().ToByteArray(),
PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_SCREENREADERS,
PdfWriter.ENCRYPTION_AES_256);
var cp = new Org.BouncyCastle.X509.X509CertificateParser();
var certChain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(certs[0].RawData) };
sap.SetCrypto(null, certChain, null, PdfSignatureAppearance.WINCER_SIGNED);
BaseFont helvetica = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(helvetica, 12, iTextSharp.text.Font.NORMAL);
sap.Layer2Font = font;
var dic = new PdfSignature(PdfName.ADOBE_PPKMS, PdfName.ADBE_PKCS7_SHA1);
dic.Date = new PdfDate(sap.SignDate);
dic.Name = certs[0].Subject;
if (sap.Reason != null)
{
dic.Reason = sap.Reason;
}
if (sap.Location != null)
{
dic.Location = sap.Location;
}
sap.CryptoDictionary = dic;
int csize = 4096;
var reservedSpace = new Dictionary<PdfName, int>();
reservedSpace[PdfName.CONTENTS] = csize * 2 + 2;
sap.PreClose(reservedSpace);
System.Security.Cryptography.HashAlgorithm sha = new System.Security.Cryptography.SHA256CryptoServiceProvider();
var sapStream = sap.GetRangeStream();
int read = 0;
byte[] buff = new byte[8192];
while ((read = sapStream.Read(buff, 0, 8192)) > 0)
{
sha.TransformBlock(buff, 0, read, buff, 0);
}
sha.TransformFinalBlock(buff, 0, 0);
System.Security.Cryptography.Pkcs.ContentInfo contentInfo = new System.Security.Cryptography.Pkcs.ContentInfo(sha.Hash);
System.Security.Cryptography.Pkcs.SignedCms signedCms = new System.Security.Cryptography.Pkcs.SignedCms(contentInfo, false);
System.Security.Cryptography.Pkcs.CmsSigner cmsSigner = new System.Security.Cryptography.Pkcs.CmsSigner(certs[0]);
cmsSigner.IncludeOption = System.Security.Cryptography.X509Certificates.X509IncludeOption.ExcludeRoot;
if (!String.IsNullOrEmpty(textBox_ServerCasovehoRazitka.Text.Trim()))
{
try
{
System.Security.Cryptography.AsnEncodedData timeData = new System.Security.Cryptography.Pkcs.Pkcs9AttributeObject(ID_TIME_STAMP_TOKEN,
DejCasoveRazitko(stream.GetBuffer(), textBox_ServerCasovehoRazitka.Text)
);
cmsSigner.UnsignedAttributes.Add(timeData);
}
catch (Exception e)
{
MessageBox.Show("Chyba TSA!\n" + e.Message);
}
}
signedCms.ComputeSignature(cmsSigner, false);
byte[] pk = signedCms.Encode();
byte[] outc = new byte[csize];
Array.Copy(pk, 0, outc, 0, pk.Length);
PdfDictionary certificateDictionary = new PdfDictionary();
sap.Close(certificateDictionary);
stp.Close();
reader.Close();
byte[] signedData = stream.GetBuffer();
File.WriteAllBytes(SouborPDF, signedData);
}
}
static protected byte[] DejCasoveRazitko(byte[] PDF, string ServerTSA)
{
System.Security.Cryptography.SHA256Managed hashString = new System.Security.Cryptography.SHA256Managed();
string hex = "";
var hashValue = hashString.ComputeHash(PDF);
foreach (byte x in hashValue)
{
hex += String.Format("{0:x2}", x);
}
NotservisTSA.GetTimeStampRequest Request = new NotservisTSA.GetTimeStampRequest(new NotservisTSA.GetTimeStampRequestBody(hex, 0));
NotservisTSA.Print2PDF_WebServiceSoap Soap = new NotservisTSA.Print2PDF_WebServiceSoapClient();
((NotservisTSA.Print2PDF_WebServiceSoapClient)Soap).Endpoint.Address = new System.ServiceModel.EndpointAddress(new Uri(ServerTSA + "/Default.asmx"));
PodepsaniPDF.NotservisTSA.GetTimeStampResponse Response = Soap.GetTimeStamp(Request);
byte[] responseBytes = Encoding.ASCII.GetBytes(Response.Body.GetTimeStampResult);
if(!String.IsNullOrEmpty(Response.Body.Error))
MessageBox.Show(Response.Body.Error, "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
string base64String = Encoding.UTF8.GetString(responseBytes, 0, responseBytes.Length);
return Convert.FromBase64String(base64String);
}
|
|
|
|
|
Hi Team,
Could someone suggest ideal way to submit 100s of rows from UI to db ?
i think better approach would be to Prepare XML and send it to DB.
Any other ideas are welcome.
Thanks,
Giri
|
|
|
|
|
|
Depends on the database-server.
In the case of SQLite, you'd be better of doing 100 inserts wrapped in a transaction.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hello all. I have a background worker that executes a query against an SQL Server database. The user clicks a button and the query runs, then the results of the query go into a datagrid. The users are requesting a Cancel button for the query because the queries can take a very long time to run. So when the user clicks the Run Query button, a small form with a Cancel Button opens. If the user clicks the button, DialogueResult.Cancel is returned and I execute CancelAsync().
Meanwhile, in my DoWork thread, I am preparing to run the query. When I run the query, I execute
adapter3.Fill(GlobalData.g_queryData);
So, there is no loop. Most of the forums I have seen tell you to check Backgroundworker.CancellationPending. The problem is that I am not running in a loop.
So, can someone suggest maybe another approach to canceling a query that is currently running?
Thanks,
David
using (SqlConnection connection = new SqlConnection(GlobalStr.sqlConnStringMetadata))
{
try
{
connection.Open();
SqlTransaction Transaction = connection.BeginTransaction(IsolationLevel.ReadUncommitted);
using (SqlCommand testQuery = new SqlCommand(GlobalStr.previewQueryString, connection))
{
testQuery.CommandTimeout = m_QueryTimeout;
testQuery.Transaction = Transaction;
using (SqlDataAdapter adapter3 = new SqlDataAdapter(testQuery))
{
adapter3.Fill(GlobalData.g_queryData);
DataSetViewer dsv = new DataSetViewer();
dsv.SetSource(GlobalData.g_queryData);
string strClassification = string.Format("{0} {1}", GlobalStr.g_classification, GlobalStr.g_releaseability);
dsv.setClassification(strClassification);
Cursor.Current = Cursors.Default;
dsv.ShowDialog();
}
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("The query test failed: " + ex.Message);
return;
}
}
|
|
|
|
|
I have accomplished a similar task in the past using a variation of this code:
public sealed class ExtendedBackgroundWorker : BackgroundWorker
{
private Thread thread;
public void Stop()
{
thread?.Abort();
thread = null;
}
protected override void OnDoWork(DoWorkEventArgs eventArgs)
{
thread = Thread.CurrentThread;
try
{
base.OnDoWork(eventArgs);
}
catch (ThreadAbortException threadAbort)
{
Debug.WriteLine("ExtendedBackgroundWorker has been aborted");
Thread.ResetAbort();
eventArgs.Cancel = true;
}
}
} With this, you're going to Abort the Thread. Now, an Abort is normally a bad method to call (there are various resources you can look up about why it's not recommended), but we're going to handle it in a controlled manner. When the thread is aborted, a ThreadAbortException is raised so, in our OnDoWork handler, we want to Cancel the DoWorkEventArgs and reset the Abort so that it doesn't destabilise our system. Now, you have a background worker that can be killed from the calling code. By the way, I call the method Stop rather than Abort because while this code is carefully written to prevent Abort issues, I don't want people to get into the habit of calling Abort wherever they see thread codes. By hiding the implementation, I'm hiding the idea that Abort should be called.
This space for rent
|
|
|
|
|
Thank you Peter. I actually worked on this over my long weekend. Sorry for not getting back to you sooner.
I got it working by partly using your suggestion to abort the thread. This works because the only types of queries that will be run here are SELECT queries. So no records are actually being modified. So, no need to worry about rolling back transactions.
I am no longer using a background worker. I am just creating my own thread. I want to show my code below.
Class Level Code:
CloseCancelForm();
Cancel Form
public partial class FormCancelQuery : Form
{
DialogResult result = DialogResult.OK;
public FormCancelQuery()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
result = DialogResult.Cancel;
this.Close();
}
}
Main UI Form
private void qbTestQueryBtn_Click(object sender, EventArgs e)
{
frmCancel = new FormCancelQuery();
RunQueryThread = new Thread(new ThreadStart(RunQuery));
RunQueryThread.Start();
DialogResult dr = frmCancel.ShowDialog(this);
if (dr == DialogResult.Cancel)
{
frmCancel.DialogResult = DialogResult.OK;
RunQueryThread.Abort();
}
}
private void RunQuery()
{
if (ValidQuery())
{
string queryTestString = GlobalStr.previewQueryString;
string fillFlag = "'mandafillflag'";
if (queryTestString.ToLower().Contains(fillFlag))
{
MessageBox.Show("This query requires completing the fields. Click OK to begin procedure.");
QueryFiller qf = new QueryFiller();
queryTestString = qf.FillQuery(queryTestString.ToLower(), fillFlag);
}
GlobalStr.previewQueryString = queryTestString;
selectTB.Invoke(new MethodInvoker(delegate { selectTB.Text = GlobalStr.previewQueryString; }));
whereTB.Invoke(new MethodInvoker(delegate { whereTB.Clear(); }));
fromTB.Invoke(new MethodInvoker(delegate { fromTB.Clear(); }));
orderTB.Invoke(new MethodInvoker(delegate { orderTB.Clear(); }));
try
{
examineQueryInputString(GlobalStr.previewQueryString, "", GlobalBool.g_CmplxWhereClauseChecked);
}
catch (System.Exception ex)
{
CloseCancelForm();
MessageBox.Show(ex.Message);
return;
}
try
{
GlobalData.g_queryData.Rows.Clear();
GlobalData.g_queryData.Columns.Clear();
}
catch (Exception ex)
{
GlobalData.g_queryData = new DataTable();
}
Cursor.Current = Cursors.WaitCursor;
using (SqlConnection connection = new SqlConnection(GlobalStr.sqlConnStringMetadata))
{
try
{
connection.Open();
SqlTransaction Transaction = connection.BeginTransaction(IsolationLevel.ReadUncommitted);
using (SqlCommand testQuery = new SqlCommand(GlobalStr.previewQueryString, connection))
{
testQuery.CommandTimeout = m_QueryTimeout;
testQuery.Transaction = Transaction;
using (SqlDataAdapter adapter3 = new SqlDataAdapter(testQuery))
{
adapter3.Fill(GlobalData.g_queryData);
DataSetViewer dsv = new DataSetViewer();
dsv.SetSource(GlobalData.g_queryData);
string strClassification = string.Format("{0} {1}", GlobalStr.g_classification, GlobalStr.g_releaseability);
dsv.setClassification(strClassification);
Cursor.Current = Cursors.Default;
CloseCancelForm();
dsv.ShowDialog();
}
}
connection.Close();
}
catch (Exception ex)
{
if (ex.Message != "Thread was being aborted.")
{
CloseCancelForm();
MessageBox.Show("The query test failed: " + ex.Message);
}
return;
}
}
}
else
{
CloseCancelForm();
MessageBox.Show("SQL test failed.");
}
}
private void CloseCancelForm()
{
try
{
if (frmCancel.InvokeRequired)
{
frmCancel.Invoke(new MethodInvoker(delegate { frmCancel.DialogResult = DialogResult.OK; }));
frmCancel.Invoke(new MethodInvoker(delegate { frmCancel.Close(); }));
}
else
{
frmCancel.DialogResult = DialogResult.OK;
frmCancel.Close();
}
}
catch(Exception ex)
{
string message = ex.Message;
return;
}
}
Hopefully this will help someone.
David
|
|
|
|
|
Thanks for coming back and posting your update. Glad you got it working.
This space for rent
|
|
|
|
|
Yes; the usual reaction is to figure out how to "cancel the query".
Perhaps one should first look at "tuning" the query.
The "query" is being confused with the "what it is that is submitting the query" (existentially speaking). The "query" is the "database server engine"; not some thread that submitted the query.
Is the query returning a scalar or a "set"?
If a set, is it required to be viewed and paged? Reported on?
Is virtualization an option for "paged" results.
Can the execution plan be estimated and presented before kicking off the query?
What is the nature of query? Would a data reader suffice? Can intermediate results be used to confirm kicking of further queries instead of resorting to a single "large" (poorly optimized) query (e.g. when "drilling" down).
etc....
|
|
|
|
|
Hi Gerry,
These are user created queries, so there is no tuning the query.
Yes, I guess you are correct. The query is being run by the database server engine, so I guess there is no canceling the query. All I can really do is force my program to stop waiting for the query results. I guess since the query is running in a transaction, I could try rolling back the transaction.
The query returns a set.
The query is created by the user through a query designer that we wrote in C#. It only allows the user to execute SELECT queries against tables or Views in the database. It returns a dataset with the results displayed in a datagrid. The results can then be exported to a CSV or XLSX file.
Thanks or your input.
|
|
|
|
|
The queries may be "user-created", but that does not mean they should have "free access" to the database.
I would give users "unlimited" query abilities; but with some "reasonableness" applied.
With an ERP system, it makes little sense to run queries on "all".
Based on the context (User; Department; Title; etc.), users may be required to provide a "year", "month", "department code", etc. via a query interface.
For "historical" data analysis, we would build data warehouses ("facts"; dimensions; "star" relationships) that were "optimized" for ad-hoc queries.
One simply does not unleash users on "unstructured" data without some planning (and expect performance).
|
|
|
|
|
Hi Gerry. They do not have free unlimited access to everything. We use User Roles to control what they have access to. They cannot run Action Queries or DROP or CREATE queries. If the application needs to do that, then we use an Application Role with elevated permissions. We have covered our bases I think.
Thanks,
David
|
|
|
|
|
Also, just to be safe, we have an algorithm that validates their query and looks for those action query keywords such as DROP, DELETE, INSERT, CREATE, UPDATE, etc. It will not allow anyone to run queries with those keywords. We also have the user pick the tables that they want to run their query against from a combo box. The algorithm that loads the combo box only lists certain tables.
David
|
|
|
|
|
When I said "free access", I did not mean: DELETE; DROP; etc.
I meant that a person who works in "Accounts Receivable" (AR) typically only deals with "AR" data that is current for the month, and a few previous; and NOT to xx years of data for every department in the company.
That's why one has "performance problems": users (or anybody) running "dumb" queries.
And if you need to "cancel it", it cannot have been that important.
If it WAS important, one typically creates a "canned query"; with a few "optional parameters".
Anything else is chaos; and you get blamed for the performance issues.
|
|
|
|
|
Ok Gerry, I apologize. I misunderstood you.
In our environment, our SQL Server is on a non-Internet connected machine. And the data is Analysis data. More and more data is added on a regular basis. We have many millions of rows of data in several databases. Our data analysts run queries. Sometimes they create a query incorrectly (or inefficient) . We have had cases where a user will start a query before they go home for the day, and then when they come in the next morning, it is still running.
Some of our queries consist of many views joined together and some of them use scalar functions and table valued functions which get used repeatedly. This can of course slow the query down significantly. Anyway, previously the only way to cancel a query was to End Task on the program, now the user can just click the cancel button. It works very well.
Thanks,
David
|
|
|
|
|
No need for apologies...
Different experiences.
With ERP systems, I configured the query screens based on the "questions" users wanted to ask; that always involved some combination of: Company; Department; Cost Centre; Client #; Invoice #; Work order $; Year; Month; Day; etc.
Some required fields; some optional; default values off the accounting cycle. Write once (all variations of the same but limited to "codes" in their HR profile).
Every new table / "entity" is automatically secured based on "need to know".
No "bad" queries in the "operational" world; for the "informational" needs (different ASAP), there is the data warehouse. Two worlds.
|
|
|
|
|
I want to make a tool for loging form of facebook.
In the past i was make facebook windows form application in xmpp client connection, that was working but now facebook setting change and i can not design a login form with xmpp.
I want to design a windows form application with user name and password text box for facebook, please help me.
How can i login to facebook server with windows form application
|
|
|
|
|
|
Look into OAuth2
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
HI,,
IN A FORM USER INFORMATION AND PICTUREBOX....PICTURE TAKEN FROM CAMERA...AND IN PICTURE BOX DATE TIME AND OTHER TEXT IS ON PICTURE...
ON PRINTING I WANT DATE TIME AND OTHER TEXT ON PICTUREBOX IS TO BE PRINT....
BUT ON SAVING THE PICTURE ONLY PICTURE IS TO BE SAVED NOT DATE TIME AND TEXT IS TO BE DISPLAYED IN SAVED PICTURE.
|
|
|
|
|
You need to learn how to ask a question, see the sticky at the top of the list!
Don't shout (that is typing in all caps)
Don't post in multiple forums, select the most appropriate and post ONCE.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hello all,
So I need your help to get a value from a string and put it on other field. I saw tons of exemples around, but I have several issues.
So I have a field named "Comments". I need to check if that field has some words on it. After find one of those words, I need to get the value after that word and pass it to another field named "Selo".
So far I have this code:
private string selos;
public string Selos
{
get { return selos; }
5. set
{
string[] stringToCheck = new string[] { "Selo:", "selo:", "Selos:", "selos:" };
string[] result;
if(string.IsNullOrEmpty(Comments) == true)
10. {
SetPropertyValue("Selos", ref selos, "None");
}
else
{
15. result = Comments.Split(stringToCheck, StringSplitOptions.RemoveEmptyEntries);
SetPropertyValue("Selos", ref selos, result[0]);
}
}
}
Issue 1: I filled the fields "Comments" but in VisualStudio - Watch, this field is allways null, so everything else is null. Issue 2: When I change the "Comments" data, the VS Watch recognizes changes and passes all the data in this field to the "Selos" field, which is not what I want. I just want data after stringToCheck...
Here's an exemple:
Comments: Everythings OK. Selo: Q457896; Q457897
Now, the code puts in "Selo" field: Everythings OK. Selo: Q457896; Q457897, however, it doesn't save this value in the database. What I really want is:
Field Selo - Q457896; Q457897
Please I need your help to get this working... What am I missing/doing wrong here?
Thanks
|
|
|
|
|
So if I get you correctly you want to update the property Selo if the comments property has the word "Selo" or some other pre-defined variable in it or add the first value from the comments once it has been broken up.
public class something
{
public string Comments {get; private set;}
public string Selos {get;set;}
public void SetComments(string comment)
{
string[] wordsToFind = new string[] {"Selo:", "selo:", "Selos:", "selos:"};
if(String.IsNullOrEmpty(comment) == true)
{
Selos = "None";
}
else
{
string[] result = comment.Split(wordsToFind, StringSplitOptions.RemoveEmptyEntries);
Selos = resuls[0];
}
}
}
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
So much wrong, where do I begin...
First, a caveat. There is not enough code he to get a complete picture of what you're doing and how you're using this code.
You're using a property incorrectly. You're using it as essentially a readonly property and a method to try and parse an input. I'd probably break this into a REAL readonly property with only a getter and then a method to parse the input and set the property backing field. You're not using the value supplied by the property settor.
The backing field I would probably rename to _selos to make sure its differentiated from all other mentions of it in the code.
The method to parse the input would probably be easier with a regular expression instead of using the "magic values" and very nainve method you're using. What if the comment contains "sEro:"? It's going to fail. Use a case-insensitive check instead.
Also, when you split the Comment string you're using the first element in the resulting string array, which is always going to be to first word in the Comment, in your example it's "Everything".
Free form user input is not the best idea. If you can avoid it, do so. If not, treat it like it's out to destroy the world. Don't trust it to contain anything usable at all and if it does, don't trust it to be in a format you can easily parse.
|
|
|
|
|
Hi, I'm trying to solve this error but without success. I have my interface:
public interface IAmInterface
{
string Method(string username, string password);
}
And a class which implements a Method:
public class Service : IAmInterface
{
public string Method(string username, string password)
{
return "Some string";
}
}
public class SomeClass
{
private readonly IAmInterface _name;
public SomeClass(IAmInterface userinterface)
{
_name = userinterface;
}
public void Method2
{
var name1=_name.Method("a","b");
}
}
I always get the same error when I try to call a method in Method2: object reference not set to an instance of an object.
What I'm doing wrong?
|
|
|
|
|