public void GetUserDataFromCustomerCookie() { HttpCookie authCookie = HttpContext.Current.Request.Cookies.Get(FormsAuthentication.FormsCookieName); if (authCookie == null || authCookie.Value == null) { userData = null; } else { try { FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); if (authTicket != null) { AuthData tempData = JsonConvert.DeserializeObject<AuthData>(authTicket.UserData); if (users.Keys.Contains(UserData.userPrincipleName)) { userData = users[tempData.userPrincipleName]; } } else { userData = null; } } catch (Exception ex) { string message = ex.Message; userData = null; } } }
//[Authorize] // GET: Authen public string Index() { ViewBag.nextUri = Request.QueryString["Uri"]; if (!string.IsNullOrEmpty(ViewBag.nextUri)) { ViewBag.Name = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Name).Value; ViewBag.ObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; ViewBag.GivenName = ClaimsPrincipal.Current.FindFirst(ClaimTypes.GivenName).Value; ViewBag.Surname = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Surname).Value; ViewBag.UPN = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn).Value; AuthData newUser = new AuthData(); newUser.userPrincipleName = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn).Value; newUser.userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; ViewBag.Key = Guid.NewGuid().ToString(); IDsDic.Add(ViewBag.Key, newUser); //Session["RelyingApp"] = Request.QueryString["Uri"]; //Response.Cookies["RelyingApp"].Value = Request.QueryString["Uri"]; //Response.Cookies["RelyingApp"].Expires = DateTime.Now.AddDays(1); //will do similar logistics for authentication integration string uri = Request.QueryString["Uri"] + "?AK=" + ViewBag.Key; Response.Redirect(uri); } return "Welcome to ITALite."; }
static void callRESTAPI() { using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://*****:*****@jianwmfatest.partner.onmschina.cn"; inputData.appObjectId = "1a7249e7-fa56-4c47-83de-5048097bc510"; inputData.appName = "Console App for Azure AD"; inputData.hashKey = DateTime.Now.Ticks.ToString(); var requestJson = JsonConvert.SerializeObject(inputData); HttpContent httpContent = new StringContent(requestJson); httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var result = client.PostAsync("api/auth/check", httpContent).Result.Content.ReadAsStringAsync().Result; Console.WriteLine(result); AuthData outputData = JsonConvert.DeserializeObject<AuthData>(result); } }
public void ClearCookieAndLogoff() { HttpContext.Current.Response.Cookies.Clear(); System.Web.HttpCookie authCookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName); authCookie.Expires= DateTime.Now.AddDays(-1d); System.Web.HttpContext.Current.Response.Cookies.Add(authCookie); AuthData authData = GetUserCookie(); if (authData != null) { users.Remove(authData.userPrincipleName); } this.userData = null; }
public void Authorization(string appId,string appName,string hashKey) { using (var client = new HttpClient()) { client.BaseAddress = new Uri(ConfigurationManager.AppSettings["AuthenServer"]); if(userData!=null) { try { userData.appObjectId = appId; userData.appName = appName; userData.hashKey = hashKey; var requestJson = JsonConvert.SerializeObject(userData); HttpContent httpContent = new StringContent(requestJson); httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var result = client.PostAsync("api/auth/check", httpContent).Result.Content.ReadAsStringAsync().Result; userData = JsonConvert.DeserializeObject<AuthData>(result); //SetUserDataCookie(); } catch(Exception ex) { string message = ex.Message; } } } }
public void Authentication(string accesskey) { using (var client = new HttpClient()) { client.BaseAddress = new Uri(System.Configuration.ConfigurationManager.AppSettings["AuthenServer"]); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var result = client.GetAsync("api/auth/connect/?AuthenKey=" + accesskey).Result.Content.ReadAsStringAsync().Result; try { userData = JsonConvert.DeserializeObject<AuthData>(result); SetUserDataCookie(); } catch(Exception) { ; } } }