簡體   English   中英

有沒有辦法在不使用前端javascript的情況下使用c#使用RestAPI登錄到Asp.net MVC Web應用程序?

[英]Is there any way to consume RestAPI login to Asp.net MVC web application with c# without using front-end javascript?

我在 asp.net mvc 視圖中有一個登錄/索引視圖

@{
ViewData["Title"] = "Index or login page";
}

<div class="text-center">
    <label>Email Address</label>
    <input type="email" id="txtemail" placeholder="Enter your email" />

    <br/>

    <label>Password</label>
    <input type="password" id="txtpass" placeholder="Enter your email" />


    <button type="button" id="btnLogin">Login</button>
</div>

我的模型類中有以下數據

namespace test.Models
{
public class Login_Model
{
    public string email { get; set; } //you get to see the user email.
    public string name { get; set; } //user name 
    public Product[] products { get; set; } //list of products
    public string result { get; set; } // success/unsuccess
    public string message { get; set; } 
}
public class Product
{
    public string billingcycle { get; set; }
    public string nextduedate { get; set; }
    public int pid { get; set; }
    public string product_name { get; set; }
    public string product_package { get; set; }
    public string regdate { get; set; }
    public string status { get; set; }
    public string username { get; set; }
}
}

我面臨的問題是,使用該模型類,我獲得了兩個參數“uemail”和“upwd”(未在我的模型類中公開)用於登錄並生成結果和產品詳細信息。 但是,當我在控制器中將 Login_Model 類作為 [HttpPost] 方法的參數傳遞時,我無法將“uemail”和“upwd”與我的用戶輸入進行比較。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using test.Models;
using RestSharp;

namespace test.Controllers
{
public class HomeController : Controller
{
    private string baseUrl = "https://webservice.sample.com/";
    private RestClient client;
    private RestRequest Request;
    private IRestResponse response;

    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    public IActionResult Index()
    {
        return View();
    }
    [HttpPost]
    public IActionResult Index(Login_Model login)
    {
         client = new RestClient(baseUrl);
        Request = new RestRequest("login.php", Method.POST);


        return RedirectToAction("Privacy");
    }


    public IActionResult Privacy()
    {
        return View();
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
}
}

在 asp.net mvc 中獲取字符串 json.result == "success" 以將我的索引視圖重定向到隱私視圖的最佳方法是什么。

是的,您可以使用 HttpWebRequest 類來調用 rest APi

string url = "https://api.xxxxx.com/v1/login/";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Headers.Add("API-key", "your-api-key-if-any");
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)";
request.Accept = "/";
request.UseDefaultCredentials = true;
request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.  
request.ContentLength = byteArray.Length;

// Get the request stream.  
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.  
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.  
dataStream.Close();


HttpWebResponse resp = request.GetResponse() as HttpWebResponse;
using (var streamReader = new StreamReader(resp.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM