簡體   English   中英

如何將登錄屏幕連接到特定的網站和數據庫以接受用戶名和密碼?

[英]How do I connect my login screen to a specific website & database to accept usernames and passwords?

我正在為我正在使用的應用程序創建登錄頁面,為了使登錄頁面更完整,我遵循了本教程( http://dipinkrishna.com/blog/2012/03/iphoneios-programming-login-screen- post-data-url-parses-json-response / )。 我想知道為什么我創建的登錄頁面無法正常工作。 我很確定這可能與將數據庫連接到應用程序有關。 關於如何執行此操作的任何線索? 我知道用戶名和密碼有效,因為我在網站上嘗試了它,並且效果很好。 關於為什么它在應用程序中不起作用的任何線索? 謝謝您的幫助 !

這是我的代碼片段:

ViewController.m

- (IBAction)loginClicked:(id)sender {
    @try {

        if([[username text] isEqualToString:@""] || [[password text] isEqualToString:@""] ) {
            [self alertStatus:@"Please enter both Username and Password" :@"Login Failed!"];
        } else {
            NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[username text],[password text]];
            NSLog(@"PostData: %@",post);

            NSURL *url=[NSURL URLWithString:@"http://thespot2g.com/login"];

            NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

            NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
            [request setURL:url];
            [request setHTTPMethod:@"POST"];
            [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
            [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
            [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
            [request setHTTPBody:postData];

            //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

            NSError *error = [[NSError alloc] init];
            NSHTTPURLResponse *response = nil;
            NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

            NSLog(@"Response code: %d", [response statusCode]);
            if ([response statusCode] >=200 && [response statusCode] <300)
            {
                NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
                NSLog(@"Response ==> %@", responseData);

                SBJsonParser *jsonParser = [SBJsonParser new];
                NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
                NSLog(@"%@",jsonData);
                NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
                NSLog(@"%d",success);
                if(success == 1)
                {
                    NSLog(@"Login SUCCESS");
                    [self alertStatus:@"Logged in Successfully." :@"Login Success!"];

                } else {

                    NSString *error_msg = (NSString *) [jsonData objectForKey:@"error_message"];
                    [self alertStatus:error_msg :@"Login Failed!"];
                }

            } else {
                if (error) NSLog(@"Error: %@", error);
                [self alertStatus:@"Connection Failed" :@"Login Failed!"];
            }
        }
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);
        [self alertStatus:@"Login Failed." :@"Login Failed!"];
    }
}

- (IBAction)backgroundClick:(id)sender {
    [username resignFirstResponder];
    [password resignFirstResponder];

}

這是我無法使用正確的憑據登錄后進入控制台的內容

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <meta http-equiv="content-language" content="en-US">
  <meta name="author" content="Innovatrix Labs, LLC">
  <meta name="copyright" content="The Spot2g, LLC">  
  <meta name="description" content="connecting nightlife enthusiasts with their favorite venues through a real time feed of drink specials, ent, waits, cover charges, and etc!">
  <meta name="keywords" content="The Spot2g, Spot2g, Bar Specials, Drink Specials, Awesome Venues, Innovatrix Labs">
  <meta name="city" content="State College, Williamsport">
  <meta name="state" content="Pennsylvania">
  <title>The Spot2g</title>
  <link rel="shortcut icon" href="http://thespot2g.com/favicon.ico"/>
  <link rel="stylesheet" type="text/css" href="http://thespot2g.com/styles/spot2g.css"/>
  <link rel="stylesheet" type="text/css" href="http://thespot2g.com/styles/bootstrap.css"/>
  <script src="http://thespot2g.com/scripts/jquery-1.8.2.min.js"></script>
  <script src="http://thespot2g.com/scripts/jquery.accordion.min.js"></script> 
  <script src="http://thespot2g.com/scripts/homepage.js"></script>
  <script src="http://thespot2g.com/scripts/loginajax.js"></script>
  <script src="http://thespot2g.com/scripts/jquery.timeago.js"></script>
  <script src="http://thespot2g.com/scripts/bootstrap.min.js"></script>
<script type="text/javascript">
// Google Analytics 
 var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-26344176-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

  </head>
<body>
 <div id="page-wrapper">
  <div id="header">
   <div class="header-wrap">
    <div class="header-wrap-2">     
     <div class="logo">
      <a href="http://thespot2g.com/home"><img src="http://thespot2g.com/images/logo.png" style="background-repeat:no-repeat;" alt="Spot2G" /></a>
     </div> <!-- logo -->
        <div id="navigation-top">
<div class="user-controls"> 
 <ul>
   <li><a href="http://thespot2g.com/login">Log In</a></li> | 
   <li><a href="http://thespot2g.com/account/register">Register</a></li>
  </ul>
</div><!--//user-controls -->

</div><!--//navigation-top -->
    </div><!--//header-wrap-2 -->
   </div><!--//header-wrap -->

  </div><!-- //#header -->
  <div class="content-wrapper">
  <div class="featured-special">
    <p> <a href="http://thespot2g.com/venues/id/38">Follow us on twitter: @TheSpot2g !! at TheSpot2g</a> </p> 
  </div>


  <div id="content">
    <div id="content-left">
    <div id="navigation-side">
    <div class="navigation">
     <ul>
      <li class="accordion-parent"><a href="#">Venues</a></li>
      <ul class="accordion-content" style="max-height:235px; overflow-x:hidden;">

        <li><a href="/venues/id/61">Arena Bar & Grill</a></li><li><a href="/venues/id/68">Autoport</a></li><li><a href="/venues/id/34">Bar Bleu</a></li><li><a href="/venues/id/71">Brewery</a></li><li><a href="/venues/id/46">Cafe 210</a></li><li><a href="/venues/id/59">Champs Sports Grill</a></li><li><a href="/venues/id/31">Chrome</a></li><li><a href="/venues/id/30">Darkhorse</a></li><li><a href="/venues/id/51">Gigi's</a></li><li><a href="/venues/id/47">Gingerbread Man</a></li><li><a href="/venues/id/26">Hi-way Pizza</a></li><li><a href="/venues/id/25">Home Delivery Pizza</a></li><li><a href="/venues/id/56">Indigo</a></li><li><a href="/venues/id/23">Inferno</a></li><li><a href="/venues/id/42">Kildare's</a></li><li><a href="/venues/id/70">Levels</a></li><li><a href="/venues/id/57">Lions Den</a></li><li><a href="/venues/id/19">Mad Mex</a></li><li><a href="/venues/id/43">Mt. Nittany Inn</a></li><li><a href="/venues/id/18">Otto's Pub and Brewery</a></li><li><a href="/venues/id/50">Phyrst</a></li><li><a href="/venues/id/64">Phyrst Upstairs</a></li><li><a href="/venues/id/16">Pickles</a></li><li><a href="/venues/id/60">Pletcher's Beer Distributor</a></li><li><a href="/venues/id/55">Rotelli's</a></li><li><a href="/venues/id/63">Shandygaff</a></li><li><a href="/venues/id/11">Sharkies</a></li><li><a href="/venues/id/15">The Rathskellar</a></li><li><a href="/venues/id/13">The Saloon</a></li><li><a href="/venues/id/38">TheSpot2g</a></li><li><a href="/venues/id/58">W.R. Hickey Beer Distributor</a></li><li><a href="/venues/id/62">Westside Stadium Bar and Grill</a></li><li><a href="/venues/id/8">Z Bar</a></li><li><a href="/venues/id/7">Zeno's</a></li>      </ul>
      <li class="accordion-parent"><a href="#">Whats Going On?</a></li>
        <ul class="accordion-content">
            <li><a href="/about"> What is The Spot2g? </a> </li>
            <li><a href="https://twitter.com/thespot2g" target="_blank"> Follow us on Twitter! </a></li>
            <li><a href="http://www.facebook.com/pages/The-Spot2g-LLC/160013764093417" target="_blank"> Like us on Facebook! </a> </li> 
        </ul>
     </ul>
    </div> <!-- navigation -->  
</div><!--//#navigation-side --> 
 <div class="content-box">
     <a href="http://www.blaisealexanderhyundaimazda.com/" target="_blank"><img src="http://thespot2g.com//images/uploads/promo/1361154289.jpg" width="230px"/></a>
 </div><!--//content-box -->


 <div class="content-box">
     <a href="http://www.wrhickey.com" target="_blank"><img src="http://thespot2g.com//images/uploads/promo/1363833306.jpg" width="230px"/></a>
 </div><!--//content-box -->


</div><!--//#content-left -->
  <div id="main-content">   
    <div id="form-login">
<form action="http://thespot2g.com/login" method="post" accept-charset="utf-8"><div class="form-title"><p>Login to the Spot2g</p></div>
<ul>
    <li>
        <label>Username</label>
        <div><input type="text" name="login-username" value="" id="login-username"  /></div>
    </li>
    <li>
        <label>Password</label>
        <div><input type="password" name="login-password" value="" id="login-password"  /></div>
    </li>
    <li id="login-submit">
        <div><input type="submit" name="login" value="Login"  /></div>
    </li>
    <li>
        <p>The Username field is required.</p>
<p>The Password field is required.</p>
    </li>
</ul>   
</form> 
</div><!--//#form-login -->
  </div><!-- //#main-content -->
    <div id="content-right">
 <div class="content-box">
  <h2> Featured Venue </h2>
        <a href="/venues/id/47"> <img src="/images/uploads/1361328262.jpg" width="230px"/> </a>
 </div><!--//content-box-->

 <div class="content-box">
     <a href="http://www.pletchersbeer.com" target="_blank"><img src="http://thespot2g.com//images/uploads/promo/1364503058.jpg" width="230px"/></a>
 </div><!--//content-box -->

 <div class="content-box">
     <a href="https://www.facebook.com/pages/Good-Evil-Tattoos/105461496181423?ref=ts&fref=ts" target="_blank"><img src="http://thespot2g.com//images/uploads/promo/1377425552.jpg" width="230px"/></a>
 </div><!--//content-box -->

 <div class="content-box">
     <a href="http://www.michaelrobertssilverscissors.com/" target="_blank"><img src="http://thespot2g.com//images/uploads/promo/1379012957.jpg" width="230px"/></a>
 </div><!--//content-box -->

</div><!--//#content-right -->
 </div><!--//#content -->
    </div><!--//#content-wrapper -->
    <div id="footer">  
        <p>&copy; 2012 <a href="http://thespot2g.com/">The Spot2g, LLC</a> All Rights Reserved.</p>
        <p>Powered by <a href="http://www.innovatrixlabs.com">Innovatrix Labs</a></p>
   </div><!-- //#footer -->
  </div> <!-- //#page-wrapper -->
 </body>
</html>

2013-10-28 23:55:24.281 Spot2G[1069:a0b] (null)
2013-10-28 23:55:24.282 Spot2G[1069:a0b] 0

我不確定您的特定問題,但可以提供一些一般建議:

使用OAuth

為了節省用戶在每次使用該應用程序時都必須使用用戶名和密碼憑據登錄的情況,您可能需要考慮使用OAuth安全協議來保護應用程序的安全。 這就是Facebook,Twitter,Dropbox,Instagram等的工作方式。

OAuth協議的基礎是:

  • 用戶使用其用戶名和密碼憑據進行一次身份驗證。
  • 身份驗證提供程序為應用程序分配令牌,該令牌可用於后續請求。
  • 該令牌有一個有效期。 用戶也可以通過訪問站點將其撤消,以防設備丟失時使其安全。

您可以使用例如GTMOAuth2在后端和使用者上設置自己的OAuth提供程序。 您還可以將上述方法之一(Facebook,Dropbox,Instagram等)用作單點登錄提供程序。

單點登錄:

  • 使用客戶端SDK(Facebook,LinkedIn等)或通用OAuth庫獲取訪問令牌。
  • 將此訪問令牌發送到后端。
  • 后端將嘗試使用令牌進行身份驗證。 如果成功,則分配一個會話x分鍾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM