-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuth_Stu.aspx.cs
97 lines (90 loc) · 3.64 KB
/
Auth_Stu.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Security;
using Newtonsoft.Json;
public partial class Auth_Stu : System.Web.UI.Page
{
private static TimeSpan autoUnlock = TimeSpan.FromHours(0.5);
public static HttpCookie Set_Cookie(string str)
{
var sb = str.Split(';');
if (str.IndexOf("webpy_session_id") != -1)
{
HttpCookie cookie = new HttpCookie("webpy_session_id");
string value = sb[0].Substring(17);
cookie.HttpOnly = true;
cookie.Path = "/";
cookie.Value =value;
return cookie;
}
return null;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string strId = Request.QueryString["stuno"];
string strPassword = Request.QueryString["password"];
strId = Request.Form["stuno"] != "" ? Request.Form["stuno"] : strId;
strPassword = Request.Form["password"] != "" ? Request.Form["password"] : strPassword;
string ret = string.Empty;
string str = "stuno=" + strId + "&" + "password="+strPassword;
ASCIIEncoding encoding = new ASCIIEncoding();
try
{
byte[] byteArray = encoding.GetBytes(str); //转化
HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri("http://stu.fudan.edu.cn/user/login"));
webReq.Method = "POST";
webReq.ContentType = "application/x-www-form-urlencoded";
webReq.AllowAutoRedirect = false;
webReq.ContentLength = byteArray.Length;
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);//写入参数
newStream.Close();
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
string py_cookie=response.GetResponseHeader("Set-Cookie");
HttpCookie auth_cookie=Set_Cookie(py_cookie);
var result = Darili_User.Validate_StuCommon(auth_cookie);
if (result.Item1 == true)
{
if (!Darili_User.IsInitialized(result.Item3))
{
//初始化本地用户数据库
int uid = Darili_User.Get_StuId(auth_cookie);
string nickname = result.Item3;
if(Darili_User.Initialize(nickname, uid)!=uid)throw new Exception();
}
Darili_User.RecordLoginTime(result.Item3);
RedictFromLoginPage(result.Item3, result.Item2, auth_cookie);
}
else
{
//未完成,暂时跳白屏
}
// Response.Write(result);
/*StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
ret = sr.ReadToEnd();
sr.Close();
response.Close();
newStream.Close();*/
}
catch (Exception ex)
{
}
}
}
private void RedictFromLoginPage(string nickname, string stuno,HttpCookie webpy)
{
HttpCookie cookie = new HttpCookie("stuno");
cookie.HttpOnly = true;
cookie.Value = stuno;
Response.Cookies.Add(cookie);
Response.Write("");
Response.AppendCookie(webpy);
FormsAuthentication.RedirectFromLoginPage(nickname, false);
}
}