コントローラークラスにログイン機能つけてみた
コントローラークラス
単一のControllerでログイン、ログアウトを行えるようにAccountModelsでの定義クラスを使用してやってみた。
AccountModelsのソースはいじってません。
AccountModelsのソースはいじってません。
public class HogegeController : Controller { private IMembershipService membershipService = new AccountMembershipService(); private IFormsAuthenticationService formAuthService = new FormsAuthenticationService(); public ActionResult Index() { return new EmptyResult(); } [HttpPost] public ActionResult LogOn(string userid, string password) { try { if (membershipService.ValidateUser(userid, password)) { formAuthService.SignIn(userid, false); return Content("logon ok"); } } catch (Exception exc) { Console.WriteLine(exc.ToString()); } return new HttpUnauthorizedResult(); } public ActionResult LogOut() { formAuthService.SignOut(); return Content("logout ok"); } [HttpGet] public ActionResult HogeResource(string extention) { //extentionパラメーターには.を含まない拡張子がセットされる。 if (Request.IsAuthenticated) { if(extention == "html") { //ブラウザへのレスポンスを想定。リソースをhtmlで表したものを返す。 return View(); } if(extention == "xml") { //各種アプリケーションへのレスポンスを想定。リソースをxmlで表したものを返す。 return new ContentResult() { ContentType = "Application/xml", Content = "<resource>リソース</resource>" }; } } return new EmptyResult(); } }
Webページへログインし、リソースを取得するWindowsアプリケーション
どうなんだろうかこの方法は?
public partial class Form1 : Form { public Form1() { InitializeComponent(); } //ログイン private void btnLogIn_Click(object sender, EventArgs e) { var encodedUserId = WebUtility.HtmlDecode(txtUserId.Text.Trim()); var encodedPassword = WebUtility.HtmlDecode(txtPassword.Text.Trim()); var contentBody = String.Format("userid={0}&password={1}", encodedUserId, encodedPassword); var utf8 = new UTF8Encoding(); var bytes = utf8.GetBytes(contentBody); var req = HttpWebRequest.Create("http://localhost/Hogege/LogOn"); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.ContentLength = bytes.Length; req.GetRequestStream().Write(bytes, 0, bytes.Length); var res = req.GetResponse(); var cookie = res.Headers["Set-Cookie"].ToString().Split(new[]{ "; " }, StringSplitOptions.RemoveEmptyEntries); this.aspxauth = cookie[0]; var rs = new StreamReader(res.GetResponseStream()); Console.WriteLine("[{0}] {1}", DateTime.Now, rs.ReadToEnd()); rs.Close(); } private string aspxauth = null; //ログアウト private void btnLogOut_Click(object sender, EventArgs e) { var req = HttpWebRequest.Create("http://localhost/Hogege/LogOut"); req.Method = "GET"; var res = req.GetResponse(); var rs = new StreamReader(res.GetResponseStream()); Console.WriteLine("[{0}] {1}", DateTime.Now, rs.ReadToEnd()); rs.Close(); aspxauth = null; } //リソースの取得 private void btnReq_Click(object sender, EventArgs e) { if (aspxauth == null) { MessageBox.Show("ログインしてください"); return; } var req = HttpWebRequest.Create("http://localhost/Hogege/HogeResource.xml"); req.Method = "GET"; req.Headers.Add("Cookie", aspxauth); var res = req.GetResponse(); var rs = new StreamReader(res.GetResponseStream()); Console.WriteLine("[{0}] {1}", DateTime.Now, rs.ReadToEnd()); rs.Close(); } }