7 Replies Latest reply on Sep 27, 2017 11:54 AM by Rick Davin

    Login screen to PI Server  through C#

    jag_69

      How to call the login screen through C#?

      I want to get login screen for user.

        • Re: Login screen to PI Server  through C#
          Rick Davin

          Hi Janusz,

           

          There are a number of ways to do this with AF SDK, but the most straightforward would be PIServer.ConnectWithPrompt.  There are a few other Connect overloads for you to browse.

          1 of 1 people found this helpful
            • Re: Login screen to PI Server  through C#
              jag_69

              Hi,

              Do you have any example?

              The examples from website by me not work properly.

               

              Pozdrawiam

              Janusz Graczyk (JaG)

                • Re: Login screen to PI Server  through C#
                  David Hearn

                  What problem are you having with the examples? The example shows how to call some of the Connect methods, you just need to use the ConnectWithPrompt method similar to what is shown in the examples.

                    • Re: Login screen to PI Server  through C#
                      jag_69

                      Ok, I prepare form and write the code. All is right when I logon correctly on correct user and password. My problem is when I logon with wrong password. My programm hang on. I don't cach the status, error how is in this moment. Any hint ? Is there a built-in login screen? Just like in Excel? Called by the method.

                        • Re: Login screen to PI Server  through C#
                          Rick Davin

                          Hi Janusz,

                           

                          Can you share the relevant code? 

                           

                          Note that C# and .NET programming features such as using try-catch blocks to capture an exception are beyond the scope of AF SDK.  That doesn't mean we won't help you (within reason).  But it would be useful to see your code in order to do so.

                              • Re: Login screen to PI Server  through C#
                                Rick Davin

                                Thanks but I didn't need the entire project. I see you have a WPF app using Visual Studio 2015 and C# 6.0.  Your application is targeting .NET Framework 4.6.

                                 

                                I see that rather than have AF SDK call the Windows Login dialog that your MainWindow is a custom login form. Your original question was regarding how to force the Windows Login dialog to appear.  You have now completely skipped that and should be passing Network Credentials to your connect method. 

                                 

                                Does your login form account allow for AD or Windows Domains?  It does not seem to allow for bad inputs.  What does your code do if someone enters the wrong username or password?

                                 

                                File: MainWindow.xaml.cs

                                 

                                Method: btnOk_Click

                                 

                                The catch does not capture any exceptions, which may prove useful for debugging.

                                 

                                            catch (Exception ex)
                                            {
                                                // Great spot for a breakpoint when you have problems.
                                                // You can now examine ex in your Locals window
                                                isLog = false;
                                                rej.btnZapisz.IsEnabled = false;
                                                rej.txtUserZalogowany.Text = "Nie ma połaczenia z PISERVER1, brak uprawnień do zapisu.";
                                            }
                                
                                

                                 

                                 

                                File: AppCode\PIConnect.cs

                                 

                                Public Fields: _mySvr and _myCurrUser

                                 

                                This fields should be properties.  The Microsoft naming convention for public properties is that the first character should be a capital letter (no underscore).  There is no need for _mySvr as it really contains the same value as piServerName, but only after a successful connection.

                                 

                                Rather than have a static assignment to _myCurrUser, you could have it be read-only property.

                                 

                                With that in mind, you would change this:

                                 

                                        string piServerName;
                                        public string _mySvr = string.Empty;
                                        public string _myCurrUser = string.Empty;
                                

                                 

                                To this:

                                 

                                        public string PiServerName { get; }
                                        public string CurrentUser => myPiServer?.CurrentUserName;
                                

                                 

                                 

                                Method: setConnectionParams

                                 

                                I would suggest renaming to ConnectWithParams to denote the full purpose of the method.  Two of the parameters (domain & piServer) in the method signature are ignored.  You don't need piServer since the class instance has myPiServer.

                                 

                                I would change this:

                                 

                                        public void setConnectionParams(string user = "", string pass = "", string domain = "", string piServer = "")
                                        {
                                            NetworkCredential credentials = new NetworkCredential(user, pass);
                                
                                            myPiServer.Connect(credentials);
                                            _mySvr = myPiServer.ConnectionInfo.PIServer.ToString();
                                            _myCurrUser = myPiServer.CurrentUserName;
                                        }
                                

                                 

                                To this:

                                 

                                        public void ConnectWithParams(string user = "", string pass = "", string domain = "")
                                        {
                                            if (string.IsNullOrWhiteSpace(domain))
                                            {
                                                ConnectWithParams(new NetworkCredential(user, pass));
                                            }
                                            else
                                            {
                                                ConnectWithParams(new NetworkCredential(user, pass, domain));
                                            }
                                        }
                                
                                        public void ConnectWithParams(NetworkCredential credentials)
                                        {
                                            if (myPiServer.ConnectionInfo.IsConnected)
                                            {
                                                return;
                                            }
                                            if (credentials == null || string.IsNullOrWhiteSpace(credentials.UserName))
                                            {
                                                myPiServer.Connect();
                                            }
                                            else
                                            {
                                                myPiServer.Connect(credentials);
                                            }
                                        }
                                

                                 

                                 

                                And despite doing all of that, keep in mind that the Connect request could fail if you have passed in bad credentials.  So back in MainWindow, you need to capture and detect when that happens. NOTE: I consider it a key duty of the developer to understand that when average user is prompted for information that you should validate the returned information or at the very least consider that it may be incorrect. 

                                 

                                What if you want to abandon your custom login form and use the standard Windows Login dialog?  You could add the following into PIConnect.cs:

                                 

                                        public void ConnectBuiltInPrompt()
                                        {
                                            myPiServer.ConnectWithPrompt(null);
                                        }
                                

                                 

                                 

                                However, since you are using WPF, I am uncertain that a WinForms ModalDialog can be displayed that easy.  If there are other readers who have done this before, please share your experience here.  Otherwise, you may have to use your custom login form.