/// <summary> /// Invoked when the application is launched normally by the end user. Other entry points /// will be used when the application is launched to open a specific file, to display /// search results, and so forth. /// </summary> /// <param name="args">Details about the launch request and process.</param> protected override void OnLaunched(LaunchActivatedEventArgs args) { Frame rootFrame = Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (rootFrame == null) { // Create a Frame to act as the navigation context and navigate to the first page rootFrame = new Frame(); if (args.PreviousExecutionState == ApplicationExecutionState.Terminated) { //TODO: Load state from previously suspended application } // Place the frame in the current Window Window.Current.Content = rootFrame; } if (rootFrame.Content == null) { // When the navigation stack isn't restored navigate to the first page, // configuring the new page by passing required information as a navigation // parameter if (!rootFrame.Navigate(typeof(MainPage), args.Arguments)) { throw new Exception("Failed to create initial page"); } } var binding = new w(); var strUrl = "http://localhost:59686/Services/AuthenticationService.svc"; var endPoint = new EndpointAddress(strUrl); var channelFactory = new ChannelFactory<IAuthenticationService>(binding, endPoint); IAuthenticationService tObject = channelFactory.CreateChannel(); AuthService.AuthenticationServiceClient client = new AuthService.AuthenticationServiceClient(); Task<bool> authResult = client.AuthenticateUserAsync("user", "password"); authResult.Wait(); var ser = new DataContractJsonSerializer(typeof(IEnumerable<EndPointDefinition>)); var strEndPointsJSON = LoadPage("http://localhost:59686/"); var byteArray = Encoding.Unicode.GetBytes(strEndPointsJSON); var mem = new MemoryStream(byteArray); mem.Position = 0; var endPointsDefinition = ser.ReadObject(mem) as IEnumerable<EndPointDefinition>; var commService = new CommunicationService.CommunicationService(endPointsDefinition); var authService = commService.GetInstance<IAuthenticationService>(); bool auth = authService.AuthenticateUser("Paulo", "password"); // Ensure the current window is active Window.Current.Activate(); }
private void GetData() { var id = System.Threading.Thread.CurrentPrincipal.Identity as ClaimsIdentity; if (id != null && id.IsAuthenticated && !string.IsNullOrWhiteSpace(id.Claims.First(v => v.Type == ClaimTypes.UserData).Value) && DateTime.Now < DateTime.Parse(id.Claims.First(v => v.Type == ClaimTypes.Expiration).Value)) { //HttpRequestMessageProperty request = new HttpRequestMessageProperty(); //request.Headers[HttpRequestHeader.Cookie] = id.Claims.First(v => v.Type == ClaimTypes.UserData).Value; //OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = request; //c.Open(); MessageBox.Show("You have been loggedin ," + id.Name + ",and your role is " + id.Claims.First(v => v.Type == ClaimTypes.Role).Value); //调用 WCF var b = new CookieBehavior(id.Claims.First(v => v.Type == ClaimTypes.UserData).Value); var c2 = new ServiceReference1.Service1Client(); c2.Endpoint.EndpointBehaviors.Add(b); var result = c2.GetData(14); MessageBox.Show(result ?? "No Value"); return; } var loginform = new Login(); if (loginform.ShowDialog() ?? false) { var c = new AuthService.AuthenticationServiceClient(); using (new OperationContextScope(c.InnerChannel)) { if (c.ValidateUser(loginform.UserName, loginform.Password, string.Empty) && c.Login(loginform.UserName, loginform.Password, string.Empty, true)) { var responseMessageProperty = (HttpResponseMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpResponseMessageProperty.Name]; string cookie = responseMessageProperty.Headers.Get("Set-Cookie"); //ASP.NET_SessionId=wx5ygqht1vdc4antol5pvq0l; path=/; HttpOnly,.ASPXAUTH=BC10A5A7CB7B83AE8B555F0ECD2DD0778272AA12C78B4A44A2D5203F96363DFA2D4EEF638F553C0B552F7D2BD414F024CE49A089D21CA1546867FBCC0FD5D0233CDAB5EF1FF990BFAA9777524364B57C6EA8E8862AE7C700E49F3013B34F9B5D306F36644684648F9D3EBE64B9017FEF; expires=Fri, 16-Oct-2015 04:30:22 GMT; path=/ //var Currentticket = Regex.Match(cookie, ".ASPXAUTH=(\\w+);").Groups[1].Value; var Currentticket = Regex.Match(cookie, ".ASPXAUTH=(\\w+);").Groups[1].Value; var CurrentExpiresTime = DateTime.ParseExact(Regex.Match(cookie, "expires=(.+GMT);").Groups[1].Value, "ddd, dd-MMM-yyyy HH:mm:ss 'GMT'", CultureInfo.InvariantCulture).ToLocalTime(); //var ticket = FormsAuthentication.Decrypt(Currentticket); var roles = Regex.Match(cookie, "Roles=(.+);").Groups[1].Value; var rolelist = UTF8Encoding.Default.GetString(Convert.FromBase64String(roles)).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); var list = new List<Claim>(); list.Add(new Claim(ClaimTypes.Name, loginform.UserName)); foreach (var role in rolelist) { list.Add(new Claim(ClaimTypes.Role, role)); } list.Add(new Claim(ClaimTypes.Expiration, CurrentExpiresTime.ToString())); list.Add(new Claim(ClaimTypes.UserData, Currentticket)); var id2 = new ClaimsIdentity(list, "WCF AuthService"); System.Threading.Thread.CurrentPrincipal = new ClaimsPrincipal(id2); MessageBox.Show("you logged in :" + System.Threading.Thread.CurrentPrincipal.Identity.Name); } else { MessageBox.Show("Login failed!"); } } } else { MessageBox.Show("You are not loggedin."); } }