1

My web built in a webview, when I start my app, it will show up a login page, after the users successfully login, it will redirect to successful login page. And I could get the cookies from the successful login page, so my question is can I use the cookies from successful login page to authenticate users login?

user3200976
  • 21
  • 1
  • 5
  • @sush Yes, I know. How? – user3200976 Jan 23 '14 at 05:31
  • by session management. send the cookies in session. – Sush Jan 23 '14 at 05:32
  • @sush Could you provide an example? I will take your answer if it helps. – user3200976 Jan 23 '14 at 05:35
  • Hi please check the Url http://stackoverflow.com/questions/21245083/storing-integers-in-preference-class/21249802#21249802. In android we are storing the local data as in SharedPreferences. Please check my exiting answer with example code. Hope it should helpful for you. Thanks. – Jebasuthan Jan 23 '14 at 05:38
  • look at this ans. hope this will help [link][1] [1]: http://stackoverflow.com/a/4225022/793943 – Sush Jan 23 '14 at 05:49

1 Answers1

0

You can get cookie from the web view like below:

WAY-1

In web view there is a method onPageFinished(), from there also you can get cookie

@Override
public void onPageFinished(WebView view, String url){
    String cookies = CookieManager.getInstance().getCookie(url);
    Log.d(TAG, "All the cookies in a string:" + cookies);
}

WAY-2

you can get the cookie value by this

getCookie("http://www.mysite.com","CookieName");

public String getCookie(String siteName,String CookieName){     
    String CookieValue = null;

    CookieManager cookieManager = CookieManager.getInstance();
    String cookies = cookieManager.getCookie(siteName);       
    String[] temp=cookies.split("[;]");
    for (String ar1 : temp ){
        if(ar1.contains(CookieName)){
            String[] temp1=ar1.split("[=]");
            CookieValue = temp1[1];
        }
    }              
    return CookieValue; 
}

Hope it will help you..!!!

Rushabh Patel
  • 3,052
  • 4
  • 26
  • 58
  • Hi Patel, I know how to get the cookies from my current webpage. But my question is after the users login a webpage, the wabpage will be redirected to a successful login webpage, so how do I use the cookies from that successful login webpage to tell my app that the users have already logged in? Because I want to start another activity after the users could login successfully. – user3200976 Jan 23 '14 at 05:50
  • its one kind of oauth you talking about.. after login you will get your cookie right? so when you get cookie, after that you can start your activity with passing your cookie string.. I guess it will work.. – Rushabh Patel Jan 23 '14 at 06:04
  • But if the users failed to login, they will still be able to get a cookies, how to distinguish the successful login webpage's cookies and failed to login webpage's cookies? – user3200976 Jan 23 '14 at 06:11
  • for that you need to make some validations..!!!because the way you are talking about, for that you need to implement oAuth then it will be easy for you.. – Rushabh Patel Jan 23 '14 at 06:14