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

David 356 Reputation points
2021-05-13T08:28:21.53+00:00

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.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,337 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 74,641 Reputation points Microsoft Vendor
    2021-05-13T09:36:21.59+00:00

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.