Skip to main content

Posts

Showing posts with the label C#

asp.net Single Sign-On(SSO) from SAP

公司的主系统是SAP的东西,有的时候老板想要一些小的新功能或是某个部门需要一个自己使用的小系统又不想买SAP的东西(贵啊),只有自己写和使用一些第三方免费或是开源的系统,这个时候就难免涉及到 Single Sign-On 。而在asp.net 2.0下是非常容易实现的。有SAP提供的“Sapsecu.dll”,“sapssoext.dll”2个dll再加上由公司SAP系统提供的"verify.pse"就足够了。 第一步 复制Sapsecu.dll到system32目录,并使用Rersrv32注册。 第二步 在项目里添加对sapssoext.dll的引用,vs2005会自动生成Interop.SAPSSOEXT.dll来让.net使用。 第三步 在你项目的根目录创建sap目录,并将SAP系统那边提供的verify.pse复制过去。位置倒到不是一定要在这里,只要你找得到就好。 第四步 在SAP系统那边,需要生成一个类似:http://yourserver/login.aspx?sso=werwerwerwe的链接,当然具体的页面需要你和那边的管理员商量来决定。这个链接是有时效性的,我在做测试的时候他们给的测试链接通常只能用一天。现在我们要做到就是我们这边的编码,在login.aspx的page_load里面实现: protected void Page_Load(object sender, EventArgs e) {   string sso= Request.QueryString["sso"];   SAPSSOEXT.SSO2Ticket objSSO = new SAPSSOEXT.SSO2Ticket();   objSSO.CryptLib = "sapsecu.dll";   string strKeypath = Server.MapPath("~/sap/verify.pse");    //放在根目录比较容易找   object tt;   tt = objSSO.EvalLogonTicket(strTicket, strKeypath, String.Empty); ...

Control.ResolveUrl

public string ResolveUrl (   string relativeUrl ) If the relativeUrl parameter contains an absolute URL, the URL is returned unchanged. If the relativeUrl parameter contains a relative URL, that URL is changed to a relative URL that is correct for the current request path, so that the browser can resolve the URL. 为什么要用到这个函数,我开发用vs2005,调试的时候会自动启动开发服务器,用http://localhost:xxxx/来访问。而部署的时候是用http://1.1.1.1/appname/来访问,好几个script和图片的路径不得改来改去。服务器离我几公里,而且我还没有权限用remote连接,操作服务器时必须在机房并有Admin的人陪同。要是有问题,哭都来不急。只用用这个函数加后台输出的方式来搞定。

GUID.ToString()的几种输出格式

普通的 GUID.ToString() 和 GUID.ToString("D") 输出为24个字符,有'-'分隔符的字符串,例:b3584f6b-1f97-4be1-a94f-df35d496bee4 GUID.ToString("N") 输出为20个字符,无分隔符的字符串,例:cea2081a057840dbbc30301941920e50 GUID.ToString("B") 输出为26个字符,有'-'分隔符的字符串,两端有"{}",例:{20713c2a-594f-4c80-a787-03ab2732b540} GUID.ToString("P") 输出为26个字符,有'-'分隔符的字符串,两端有"()",例:(b612d214-67fc-4285-9950-a1163a779e3d)

.net 2.0哈希字符串

存储用户密码到数据库,一般都要求先对密码字符串加密。在.net中常用的是MD5、SHA等不可逆的HASH算法。 using System.Security.Cryptography; private string EncodePassword(string password, string hashType) {   HashAlgorithm hash = HashAlgorithm.Create(hashType);     byte[] bytes = Encoding.Unicode.GetBytes(password);     byte[] inArray = hash.ComputeHash(bytes);   return Convert.ToBase64String(inArray); } 可选的HASH类型有:"SHA","SHA1","MD5","SHA256","SHA384","SHA512" 好像通杀: 2.0, 1.1, 1.0。我在2.0下测试通过。

asp.net 1.1(C#)上传文件并直接读取

aspx部分: <form id="Form1" method="post" runat="server" enctype="multipart/form-data" > <INPUT type="file" id="fileContent" name="fileContent" enctyperunat="server" > <asp:Button id="btnUpload" runat="server" Text="Upload"></asp:Button> aspx.cs部分 using (StreamReader sr = new StreamReader(fileContent.PostedFile.InputStream)) {     while( sr.Peek() >= 0 )     {         sr.ReadLine();         //to do     } } 上面加代码的中红色加亮的部分是我觉得需要特别注意的地方。asp.net上传文件本来就是很简单,这次我遇到的情况是不能存储到服务器的硬盘上,需要直接对文件进行解析。 StreamReader 确是最佳的选择。

Windows Services写日志的权限问题

在.net framework 1.1中使用以下代码判断并在系统中创建一个日志分类: if (!System.Diagnostics.EventLog.SourceExists("SourceName")) { System.Diagnostics.EventLog.CreateEventSource("SourceName", "LogName"); } 要写记录到系统日志: System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog(); eventLog.Source = "SourceName"; eventLog.Log = "LogName"; eventLog.WriteEntry("Message."); 在Windows Services中使用前面的代码创建日志分类的时候往往会遇到权限不够,这个时候可选择将ProjectInstaller所属ServiceProcessInstaller的Account设为 LocalSystem 来提高运行时的权限等级。

BindingSource

在编写一个小工具的时候需要将一个自定义的集合绑定到DataGridView。DataGridView是.net 2.0中 新的一个网格控件 。DataGridView的功能非常的强大,我在使用中确遇到一个小小的问题。 我自定的集合如下: public class CustomClassCollection : System.Collections.Generic.List<CustomClass> { } CustomClass本身是有两个String类型的属性(Propertie)。我在WinForm的构造函数里面: CustomClassCollection ccl = new CustomClassCollection(); dataGridView.DataSource = ccl; 虽然还没有数据,但是在DataGridView里面已经显出2个Title,即CustomClass的那2个属性,然后却遇到的个问题:我通过一个button Click事件向ccl里添加了东西,却没有找到DataGrid里面的DataBind()事件,而且dataGridView并没有自动的更新。DataGridView的Refresh()似乎和更新数据也没有关系。 在网上查找资料,找到 BindingSource 这个类。只需要,先将cll Bind到BindingSource上,再将BindingSource作为数据源Bind到DataGridView。向cll添加数据以后调用BindingSource的ResetBindings()就可以更新DataGridView: 先声明一个BindingSource: BindingSource bs = new BindingSource(); 再调用: bs.DataSource = ccl; dataGridView.DataSource = bs; 在添加数据的方法的最后调用: bs.ResetBindings(false); Key Word: DataGridView BindingSource 数据同步 更新

error with DBNull

Key Word: C#, DBNull, int when i try to use (f==-1) ? DBNull : f there is an error: Type of conditional expression can't be determined because there is no implicit conversion between 'System.DBNull' and 'int' we need do like: (f==-1) ? (objec)DBNull : (object)f