📄 ssiauthaction.cs
字号:
namespace Imps.Client.Core
{
using Imps.Client;
using Imps.Client.Base;
using Imps.Client.Resource;
using Imps.Client.Utils;
using Imps.Common;
using System;
using System.Net;
using System.Threading;
using System.Xml;
public sealed class SSIAuthAction : LoginAction
{
private const string errorMessage = "登录到SSI的时候发生错误";
private const string ssiHeaderName = "ssic";
private string ssiUri;
public SSIAuthAction(Imps.Client.Core.User user, ILoginListner listener) : base(user, listener)
{
}
public override void Cancel()
{
}
public override void Clear()
{
base.User.ClearSsiCredential();
}
public override void Dispose()
{
}
public override bool Execute()
{
bool flag = false;
try
{
Cookie cookie;
this.Init();
base.NotifyMessage("开始执行SSI登录, SSI Url: " + this.ssiUri);
HttpWebRequest request = ConnectionFactory.CreateHttpWebRequest(this.ssiUri + ("&digest=" + base.User.Password), base.User, false, false);
if (request.CookieContainer == null)
{
request.CookieContainer = new CookieContainer();
}
using (HttpWebResponse rsp = ((HttpWebResponse) request.GetResponse()))
{
cookie = rsp.Cookies["ssic"];
this.ProcessResponseBody(rsp);
}
if (cookie == null)
{
throw new SSIAuthException(StringTable.MsgLoginFailed);
}
base.User.SsiCredential = cookie.Value;
if (string.IsNullOrEmpty(base.User.SsiCredential))
{
throw new SSIAuthException(StringTable.MsgLoginFailed);
}
flag = true;
try
{
base.User.DoLoadImpsUserConfiguration();
}
catch (Exception exception)
{
base.NotifyException("登录到SSI的时候发生错误", exception);
}
base.NotifyMessage("SSI登录完成");
}
catch (WebException exception2)
{
if ((--base.tryTimes > 0) && ExceptionHandler.IsRetryNeeded(exception2))
{
base.NotifyException("登录到SSI的时候发生错误", exception2);
this.Execute();
return flag;
}
HttpWebResponse response = exception2.Response as HttpWebResponse;
if (response != null)
{
string b = LoginAction.GetResultCode(LoginAction.GetResponseDOM(response));
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
if (!string.Equals("401", b))
{
base.NotifyLoginFailed("登录到SSI的时候发生错误", ExceptionType.CommonException, exception2);
return false;
}
base.NotifyLoginFailed("登录到SSI的时候发生错误", ExceptionType.SSIAuthException, new SSIAuthException("登录到SSI的时候发生错误", exception2));
return flag;
}
if (response.StatusCode == HttpStatusCode.NotFound)
{
if (!string.Equals("404", b))
{
base.NotifyLoginFailed("登录到SSI的时候发生错误", ExceptionType.CommonException, exception2);
return false;
}
base.NotifyLoginFailed("登录到SSI的时候发生错误", ExceptionType.WebException, exception2);
return flag;
}
base.NotifyLoginFailed("登录到SSI的时候发生错误", ExceptionType.WebException, exception2);
return flag;
}
base.NotifyLoginFailed("登录到SSI的时候发生错误", ExceptionType.WebException, exception2);
}
catch (SSIAuthException exception3)
{
base.NotifyLoginFailed("登录到SSI的时候发生错误", ExceptionType.SSIAuthException, exception3);
}
catch (ThreadAbortException exception4)
{
base.NotifyWarning("登录到SSI的时候发生错误", exception4);
}
catch (Exception exception5)
{
base.NotifyLoginFailed("登录到SSI的时候发生错误", ExceptionType.CommonException, exception5);
}
return flag;
}
private void Init()
{
this.ssiUri = base.User.Configuration.SystemSetting.ServerUriSetting.UriSsiAppSignIn;
if (string.IsNullOrEmpty(this.ssiUri))
{
throw new NullReferenceException("ssiUri should not be null.");
}
UserAccounts.AccountData currentAccount = base.User.CurrentAccount;
if (currentAccount == null)
{
throw new NullReferenceException("User.CurrentAccount should not be null.");
}
string id = currentAccount.Id;
if (string.IsNullOrEmpty(currentAccount.Password))
{
throw new Exception("password should not be null!");
}
switch (currentAccount.AccountType)
{
case UserAccounts.AccountType.SID:
long num;
long.TryParse(id, ref num);
if (num <= 0)
{
throw new Exception("sid or mobileno should not be null together");
}
this.ssiUri = string.Format("{0}?sid={1}", this.ssiUri, num);
return;
case UserAccounts.AccountType.MobileNo:
if (string.IsNullOrEmpty(id))
{
throw new Exception("sid or mobileno should not be null together");
}
this.ssiUri = string.Format("{0}?mobileno={1}", this.ssiUri, id);
return;
}
}
private void ProcessResponseBody(HttpWebResponse rsp)
{
try
{
if (rsp.ContentLength > 0)
{
XmlDocument xmlDocument = LoginAction.GetResponseDOM(rsp);
string resultCode = LoginAction.GetResultCode(xmlDocument);
if ("200".Equals(resultCode))
{
int num;
XmlNode node = xmlDocument.DocumentElement.SelectSingleNode("user");
string uriString = node.Attributes["uri"].Value;
base.User.Uri = new IicUri(uriString);
base.SaveUri();
string text3 = node.Attributes["mobile-no"].Value;
base.User.PersonalInfo.MobileNo = text3;
if (int.TryParse(node.Attributes["user-status"].Value, ref num))
{
base.User.ProvisionStatus = (UserProvisionStatus) num;
}
}
}
else
{
ClientLogger.WriteGeneral("SSI Response body is empty.", "", 0);
}
}
catch (Exception exception)
{
ClientLogger.WriteGeneral("Process SSI Response body failed.", exception.ToString());
}
}
public override bool LocalCacheHit
{
get
{
return false;
}
}
public override Imps.Client.Core.LoginStage LoginStage
{
get
{
return Imps.Client.Core.LoginStage.LoginToSSIPortal;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -