Guest User

Untitled

a guest
Dec 20th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. public class HomeController : Controller
  2.  
  3. {
  4.  
  5. public ActionResult Index()
  6.  
  7. {
  8.  
  9. Project_I_Web_Store.Models.Login obj = new Project_I_Web_Store.Models.Login();
  10.  
  11. return View(obj);
  12.  
  13. }
  14.  
  15. [HttpPost]
  16.  
  17. public async Task<ActionResult> Index(Project_I_Web_Store.Models.Login objuserlogin)
  18. {
  19.  
  20.  
  21. var display = await GetToken(objuserlogin.UserName, objuserlogin.UserPassword);
  22.  
  23.  
  24. string[] tokenandexpire = display.Split(',');
  25. if (!display.Contains("failure"))
  26. {
  27. Session["token"] = tokenandexpire[0];
  28.  
  29. }
  30. if (display != null)
  31.  
  32. {
  33.  
  34. ViewBag.Status = "CORRECT UserNAme and Password";
  35.  
  36. }
  37.  
  38. else if (display == null)
  39. {
  40.  
  41. ViewBag.Status = "INCORRECT UserName or Password";
  42.  
  43. }
  44.  
  45. else
  46. {
  47. return View(objuserlogin);
  48. }
  49.  
  50. return RedirectToAction("Index", "Home");
  51. }
  52.  
  53.  
  54.  
  55. public async Task<string> GetToken(string userName, string password)
  56.  
  57. {
  58.  
  59. WebRequest request = WebRequest.Create("http://localhost:8080/OAuth/Token");
  60. request.Method = "POST";
  61. request.ContentType = "application/Text";
  62. string postjson = String.Format("grant_type={0}&username={1}&password={2}&client_id={3}&client_secret={4}&scope={5}", "password", userName, password, "Deevita", "DeevitaSecret", 2);
  63. byte[] bytes = Encoding.UTF8.GetBytes(postjson);
  64. using (Stream stream = await request.GetRequestStreamAsync())
  65. {
  66. stream.Write(bytes, 0, bytes.Length);
  67. }
  68.  
  69. try
  70. {
  71. HttpWebResponse httpResponse = (HttpWebResponse)(await request.GetResponseAsync());
  72. string json;
  73. using (Stream responseStream = httpResponse.GetResponseStream())
  74. {
  75. json = new StreamReader(responseStream).ReadToEnd();
  76. }
  77. TokenResponseModel tokenResponse = JsonConvert.DeserializeObject<TokenResponseModel>(json);
  78.  
  79. return tokenResponse.access_token + ":" + tokenResponse.expires_in;
  80.  
  81. }
  82. catch (Exception ex)
  83. {
  84. return "failure";
  85. }
  86.  
  87. }
  88.  
  89. }
  90.  
  91. ViewBag.Title = "Index";
  92.  
  93. <h3>Login </h3>
  94.  
  95. @using (Html.BeginForm("Index", "Home"))
  96.  
  97. {
  98.  
  99. <table width="40%" cellpadding="1" cellspacing="5">
  100.  
  101. <tr>
  102.  
  103. <td colspan="2" style="color:#f00;font-size:larger">@ViewBag.Status</td>
  104.  
  105. </tr>
  106.  
  107. <tr>
  108.  
  109. <td align="right">User Name :</td>
  110.  
  111. <td>@Html.TextBoxFor(m => m.UserName, new { @style = "width:200px", @id = "txtusername" })</td>
  112.  
  113. </tr>
  114.  
  115. <tr>
  116. <td align="right">User Password :</td>
  117.  
  118. <td>@Html.PasswordFor(m => m.UserPassword, new { @style = "width:200px", @id = "txtuserpassword" })</td>
  119.  
  120. </tr>
  121.  
  122. <tr>
  123.  
  124. <td colspan="2">
  125.  
  126. <input type="submit" value="Login" title="login" onclick=" display();" />
  127.  
  128. </td>
  129.  
  130. </tr>
  131.  
  132. </table>
  133. }
Add Comment
Please, Sign In to add comment