⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sohunews.asmx

📁 asp.net经典案例资料
💻 ASMX
字号:
<%@ WebService Language="C#" Class="SohuNews" %>

using System;
using System.Text;
using System.Security.Cryptography;
using System.Collections.Specialized;
using System.Web.Services;

[WebService(Namespace="http://www.sohu.com")]
public class SohuNews
{
  [WebMethod]
  public string[] GetNews(string passport)
  {
    if(VerifyPassport(passport) == false)
      return null;

    string[] news = new string[5];
    news[0] = "Sohu new1";
    news[1] = "Sohu new2";
    news[2] = "Sohu new3";
    news[3] = "Sohu new4";
    news[4] = "Sohu new5";
    return news;
  }

  [WebMethod]
  public string Register(string user)
  {
    return MakePassport(user);
  }

  public string MakePassport(string src)
  {
    string key = src;
    if(key.Length > 8 )
      key = key.Substring(0,8);
    else if( key.Length < 8)
      key = key.PadRight(8);

    UnicodeEncoding ue = new UnicodeEncoding();
    byte[] hashSource = ue.GetBytes(key + "sohunews");
    SHA512 shaM = new SHA512Managed();
    byte[] hashResult = shaM.ComputeHash(hashSource);

    string passport = key;
    foreach(byte b in hashResult)
      passport += b.ToString();
    return passport;
  }

  public bool VerifyPassport(string passport)
  {
    try
    {
      string key = passport.Substring(0,8);

      UnicodeEncoding ue = new UnicodeEncoding();
      byte[] hashSource = ue.GetBytes(key + "sohunews");
      SHA512 shaM = new SHA512Managed();
      byte[] hashResult = shaM.ComputeHash(hashSource);

      string new_passport = key;
      foreach(byte b in hashResult)
        new_passport += b.ToString();

      for(int i=0; i<passport.Length; i++)
      {
        if( passport[i] != new_passport[i])
          return false;
      }
      return true;
    }
    catch
    {
      return false;
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -