ボイラープレートコードフォームを使用してプロジェクトテンプレートを作成しました。ログインした.net core mvc6 webappを作成しました。.Net MVC 6デスクトップアプリケーションからの承認
ログインコントローラは、次のとおりです。
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
LoginViewModelは次のとおりです。私は、ログインウェブページからログインすることができます
public class LoginViewModel
{
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
。
デスクトップの.net c#プログラムからこのプロジェクトの[Authorization]属性を持つコントローラ(APIとして)を使用したいと考えています。このため私は、ログインコントローラからクッキーを取得し、そのクッキーを使ってAPIにアクセスしようとしています。
private void btnLogin_Click(object sender, EventArgs e)
{
HttpWebRequest http = WebRequest.Create(loginUrl) as HttpWebRequest;
http.KeepAlive = true;
http.Method = "POST";
http.ContentType = "application/x-www-form-urlencoded";
var login = new LoginViewModel();
login.Email = txtUserName.Text;
login.Password = txtPassword.Text;
var postData = new JavaScriptSerializer().Serialize(login);
byte[] dataBytes = UTF8Encoding.UTF8.GetBytes(postData);
http.ContentLength = dataBytes.Length;
using (Stream postStream = http.GetRequestStream())
{
postStream.Write(dataBytes, 0, dataBytes.Length);
}
HttpWebResponse httpResponse = http.GetResponse() as HttpWebResponse;
// ITがWITH HTTP 400
// Probably want to inspect the http.Headers here first
http = WebRequest.Create(authorized) as HttpWebRequest;
http.CookieContainer = new CookieContainer();
http.CookieContainer.Add(httpResponse.Cookies);
HttpWebResponse httpResponse2 = http.GetResponse() as HttpWebResponse;
}
LoginViewModel HERE停止します
クッキーを取得するためのデスクトップソフトウェアのコードは、(追加JSONシリアライズとコピーペースト状のStackOverflow)でありますクラスにはwebappと同じプロパティがあります。
は私のロジックはOKです:
残念ながら、それは動作しません、HTTPレスポンスは400
質問ありますか?はいの場合、コードが間違っている場所を指摘できますか?
クッキーベースの認証はOKですか?快適なWeb APIにアクセスできますか?
HTTPSであれば、トラフィック量の少ないWebサービスに使用できますか?
strompathやauth0のようなサードパーティのプロバイダがなくてもそれを行うには良い方法がありますか?