question

David-7465 avatar image
0 Votes"
David-7465 asked GotluruAvinash-6623 commented

How to create identity auth cookie and set it to webview?

Hi


In my Xamarin forms app I have a hybrid web view that open an asp.net core application. When a person logs in on the WebView, Identity creates authentication cookie in the browser. So it knows the user is logged in and they can see the protected areas of the site. The session cookie is called .AspNetCore.Cookies

Could I have a login page in xamarin that creates that specific type of cookie so I can set it in the WebView and avoid logging in using the WebView ?

I would like to open the app, display the login form. call the api to check the user credentials, get the token and then create the .AspNetCore.Cookies and inject it in the browser so the user only logs in from Xamarin and not from the WebView.

Hope this makes sense.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered GotluruAvinash-6623 commented

Hello,​

Welcome to our Microsoft Q&A platform!

You can set a cookie in Webview of Xamarin Forms, you must create a custom-renderer for Webview like following code.

[assembly: ExportRenderer(typeof(WebView), typeof(CustomWebViewRenderer))]
namespace WebViewTest.Droid
{
    public class CustomWebViewRenderer : WebViewRenderer
    {
        public CustomWebViewRenderer(Context context) : base(context)
        {
            
        }

        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                 Control.Settings.JavaScriptEnabled = true;
                var webViewClient = new CustomWebViewClient();
                Control.SetWebViewClient(webViewClient);

                CookieManager cookieManager = CookieManager.Instance;
                if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
                {
                    CookieSyncManager.CreateInstance(Control.Context);
                }
                cookieManager.SetAcceptCookie(true);
               
               
                cookieManager.RemoveSessionCookie();
                cookieManager.SetCookie("http://xx.example.com", "mid=" + MySession.GetSession().sessionId + " ; Domain=.example.com");

                //get the cookie
                string cookie = cookieManager.GetCookie("http://xx.example.com");

              

            }
        }


If you have issue about creating identity AspNetCore.Cookies, please open a new thread in the asp.net forum.

Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks, I have a custom WebView in place.

If I want to get a cookie with a specific name from the custom WebView, is there an easy way to do it for Android and iOS?

0 Votes 0 ·

Due to Forms do not have method to set the cookie , we have to create a custom renderer for webview, you can create custom renderer for custom WebView

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/hybridwebview

0 Votes 0 ·

Sorry, I did not explain it correctly.

I have got a working hybrid web view renderer in my project. Currently used in Android and iOS.

Is there a way I can check if a cookie with a specific name exists in the current hybridwebview?

The hybridwebview has got a cookie called "abc" and I would like to check whether the cookie exist or not.

@LeonLu-MSFT I have edited this post because I was not making much sense.

0 Votes 0 ·
Show more comments

Hey, @LeonLu-MSFT Could you please provide a sample for ios platform for managing cookies

0 Votes 0 ·