Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- Project_I_Web_Store.Models.Login obj = new Project_I_Web_Store.Models.Login();
- return View(obj);
- }
- [HttpPost]
- public async Task<ActionResult> Index(Project_I_Web_Store.Models.Login objuserlogin)
- {
- var display = await GetToken(objuserlogin.UserName, objuserlogin.UserPassword);
- string[] tokenandexpire = display.Split(',');
- if (!display.Contains("failure"))
- {
- Session["token"] = tokenandexpire[0];
- }
- if (display != null)
- {
- ViewBag.Status = "CORRECT UserNAme and Password";
- }
- else if (display == null)
- {
- ViewBag.Status = "INCORRECT UserName or Password";
- }
- else
- {
- return View(objuserlogin);
- }
- return RedirectToAction("Index", "Home");
- }
- public async Task<string> GetToken(string userName, string password)
- {
- WebRequest request = WebRequest.Create("http://localhost:8080/OAuth/Token");
- request.Method = "POST";
- request.ContentType = "application/Text";
- 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);
- byte[] bytes = Encoding.UTF8.GetBytes(postjson);
- using (Stream stream = await request.GetRequestStreamAsync())
- {
- stream.Write(bytes, 0, bytes.Length);
- }
- try
- {
- HttpWebResponse httpResponse = (HttpWebResponse)(await request.GetResponseAsync());
- string json;
- using (Stream responseStream = httpResponse.GetResponseStream())
- {
- json = new StreamReader(responseStream).ReadToEnd();
- }
- TokenResponseModel tokenResponse = JsonConvert.DeserializeObject<TokenResponseModel>(json);
- return tokenResponse.access_token + ":" + tokenResponse.expires_in;
- }
- catch (Exception ex)
- {
- return "failure";
- }
- }
- }
- ViewBag.Title = "Index";
- <h3>Login </h3>
- @using (Html.BeginForm("Index", "Home"))
- {
- <table width="40%" cellpadding="1" cellspacing="5">
- <tr>
- <td colspan="2" style="color:#f00;font-size:larger">@ViewBag.Status</td>
- </tr>
- <tr>
- <td align="right">User Name :</td>
- <td>@Html.TextBoxFor(m => m.UserName, new { @style = "width:200px", @id = "txtusername" })</td>
- </tr>
- <tr>
- <td align="right">User Password :</td>
- <td>@Html.PasswordFor(m => m.UserPassword, new { @style = "width:200px", @id = "txtuserpassword" })</td>
- </tr>
- <tr>
- <td colspan="2">
- <input type="submit" value="Login" title="login" onclick=" display();" />
- </td>
- </tr>
- </table>
- }
Add Comment
Please, Sign In to add comment