private void GetAccessToken() { var nopAuthorizationManager = new AuthorizationManager(_settings); var authUrl = nopAuthorizationManager.BuildAuthUrl(_settings.RedirectUrl, new string[] { }); var request = WebRequest.Create(authUrl); request.Credentials = CredentialCache.DefaultCredentials; var response = request.GetResponse(); }
//TODO: it is recommended to have an [Authorize] attribute set public ActionResult Submit([FromBody] UserAccessModel model) { if (ModelState.IsValid) { try { var nopAuthorizationManager = new AuthorizationManager(model.ClientId, model.ClientSecret, model.ServerUrl); var redirectUrl = Url.RouteUrl("GetAccessToken", null, HttpContext.Request.Scheme); // "http://localhost:9388/token"; if (redirectUrl != model.RedirectUrl) { return(BadRequest()); } var convertedId = JsonConvert.SerializeObject(model.ClientId); var convertedSecret = JsonConvert.SerializeObject(model.ClientSecret); var convertedServerUrl = JsonConvert.SerializeObject(model.ServerUrl); var convertedredirectUrl = JsonConvert.SerializeObject(redirectUrl); //var convertedstate = JsonConvert.SerializeObject(state); // For demo purposes this data is kept into the current Session, but in production environment you should keep it in your database HttpContext.Session.SetString("clientId", model.ClientId); HttpContext.Session.SetString("clientSecret", model.ClientSecret); HttpContext.Session.SetString("serverUrl", model.ServerUrl); HttpContext.Session.SetString("redirectUrl", model.RedirectUrl); // This should not be saved anywhere. var state = Guid.NewGuid(); // var convertedstate = JsonConvert.SerializeObject(state); HttpContext.Session.SetString("state", state.ToString()); string authUrl = nopAuthorizationManager.BuildAuthUrl(redirectUrl, new string[] { }, state.ToString()); return(Redirect(authUrl)); } catch (Exception ex) { return(BadRequest(ex.Message)); } } return(BadRequest()); }
//TODO: it is recommended to have an [Authorize] attribute set public ActionResult Submit(UserAccessModel model) { if (ModelState.IsValid) { try { var nopAuthorizationManager = new AuthorizationManager(model.ClientId, model.ClientSecret, model.ServerUrl); var redirectUrl = Url.RouteUrl("GetAccessToken", null, Request.Url.Scheme); if (redirectUrl != model.RedirectUrl) { return(BadRequest()); } // For demo purposes this data is kept into the current Session, but in production environment you should keep it in your database Session["clientId"] = model.ClientId; Session["clientSecret"] = model.ClientSecret; Session["serverUrl"] = model.ServerUrl; Session["redirectUrl"] = redirectUrl; // This should not be saved anywhere. var state = Guid.NewGuid(); Session["state"] = state; string authUrl = nopAuthorizationManager.BuildAuthUrl(redirectUrl, new string[] { }, state.ToString()); return(Redirect(authUrl)); } catch (Exception ex) { return(BadRequest(ex.Message)); } } return(BadRequest()); }
public ActionResult Submit(UserAccessModel model) { if (ModelState.IsValid) { try { var authorizationManager = new AuthorizationManager(model.ClientId, model.ClientSecret, model.ServerUrl); var redirectUrl = Url.RouteUrl("GetAccessToken", null, Request.Url.Scheme); if (redirectUrl != model.RedirectUrl) { return(BadRequest()); } Session["clientId"] = model.ClientId; Session["clientSecret"] = model.ClientSecret; Session["serverUrl"] = model.ServerUrl; Session["redirectUrl"] = redirectUrl; // This should not be saved anywhere. var state = Guid.NewGuid(); Session["state"] = state; string authUrl = authorizationManager.BuildAuthUrl(redirectUrl, new string[] { "sms_api" }, state.ToString()); return(Redirect(authUrl)); } catch (Exception ex) { return(BadRequest(ex.Message)); } } return(BadRequest()); }