SSO-C#跨域单点登录(一)

来源:互联网 发布:python turtle安装 编辑:程序博客网 时间:2024/05/22 05:02

开篇就是定义Model,数据为先:

    public class Something         {             public const string CookieName = "User";                   public const double TimeOut = 1;         }  

    public class User          {              public string UserName { get; set; }                    public string Password { get; set; }                    public User(string userName,string password)              {                  this.UserName = userName;                  this.Password = password;              }                    public User() { }          }  

接着创建一个名为Passport的webservice项目:

       /// <summary>         /// WebService1 的摘要说明         /// </summary>         [WebService(Namespace = "http://passport.maikegroup.com")]         [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]         [System.ComponentModel.ToolboxItem(false)]         // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。          // [System.Web.Script.Services.ScriptService]         public class WebService1 : WebService         {             [WebMethod]             public string TokenGetCredence(string tokenValue)             {                 return CacheManager.GetCacheValue(tokenValue);             }                   [WebMethod]             public string CheckUser(string userName, string password)             {                 if (CheckUserLogIn(userName, password))                 {                     string token = CreateToken();                           Common.User user = new Common.User(userName, password);                     string json = Newtonsoft.Json.JsonConvert.SerializeObject(user);                           CacheManager.CacheInsert(token, json);                           return token;                 }                       return string.Empty;             }                   private string CreateToken()             {                 return Guid.NewGuid().ToString();             }                   private bool CheckUserLogIn(string userName, string password)             {                 if ((userName.Equals("1") && password.Equals("1")) || (userName.Equals("2") && password.Equals("2")))                     return true;                       return false;             }         }                         public static class CacheManager         {             public static void CacheInsert(string key, object value)             {                 //Insert存在相同的键会替换,无返回值                 //Add 存在相同的键会异常,返回缓存成功的对象                 //Cache的过期策略使用滑动过期                 HttpRuntime.Cache.Insert(key, value, null, DateTime.MaxValue, TimeSpan.FromMinutes(Common.Something.TimeOut));             }                   public static string GetCacheValue(string key)             {                 if (HttpRuntime.Cache[key] != null)                 {                     return HttpRuntime.Cache[key].ToString();                 }                 return string.Empty;             }         }  

新建一个名为LogIn的空Web应用程序,引用上面建的web服务:

新建一个default.aspx页面:

界面如下:


    public partial class _default : System.Web.UI.Page         {             private string backUrl = string.Empty;                   public string BackUrl             {                 get { return backUrl; }                 set { backUrl = value; }             }                   protected void Page_Load(object sender, EventArgs e)             {                 if (!IsPostBack)                 {                     if (Request.QueryString["backurl"] != null)                     {                         BackUrl = Request.QueryString["backurl"];                     }                 }             }                   protected void btnLogin_Click(object sender, EventArgs e)             {                 if (string.IsNullOrEmpty(TextBox1.Text.Trim()) || string.IsNullOrEmpty(TextBox2.Text.Trim()))                     return;                       UserWS.WebService1SoapClient webservice = new UserWS.WebService1SoapClient();                 string token = webservice.CheckUser(TextBox1.Text.Trim(), TextBox2.Text.Trim());                       if (!string.IsNullOrEmpty(token))                 {                     CreateCookie(token);                     if (!string.IsNullOrEmpty(backUrl))                     {                         Response.Redirect(backUrl);                     }                     else                     {                         Response.Redirect("main.aspx");                     }                 }                 else                 {                     Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "账号密码不正确");                 }             }                   protected void btnReset_Click(object sender, EventArgs e)             {                 TextBox1.Text = string.Empty;                 TextBox2.Text = string.Empty;             }                   private void CreateCookie(string value)             {                 HttpCookie tokenCookie = new HttpCookie(Common.Something.CookieName, value)                 {                     Domain = "localhost",                     Path = "/",                     Expires = DateTime.Now.AddMinutes(Common.Something.TimeOut)                 };                 Response.Cookies.Add(tokenCookie);             }               }  


原创粉丝点击