public ActionResult Login(LoginModel model) { //return RedirectToAction("Index", "UserManagement"); string token = GetToken(model); Session["OAuthToken"] = token; if (token != "") { Session["UserName"] = model.UserName; return RedirectToAction("Index", "UserManagement"); } else return RedirectToAction("Index"); }
private string GetToken(LoginModel model) { //string URI = "http://localhost:59822//oauth/token"; string URI = ConfigurationManager.AppSettings["as:TokenIssuer"].ToString(); string myParameters = "Username="******"&Password="******"&grant_type=password"; myParameters += "&client_id=414e1927a3884f68abc79f7283837fd1"; using (System.Net.WebClient wc = new System.Net.WebClient()) { wc.Headers[System.Net.HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; try { string HtmlResult = wc.UploadString(URI, myParameters); Token token = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Token>(HtmlResult); return token.access_token; } catch (Exception e) { return ""; } } }