damienbod / AspNetCoreHybridFlowWithApi

Different ASP.NET Core applications using OpenID Connect Hybrid flow Code Flow, Code Flow with PKCE, JWT APIs, MFA examples
https://damienbod.com/2018/02/02/securing-an-asp-net-core-mvc-application-which-uses-a-secure-api/
MIT License
364 stars 71 forks source link

Handle an Event and Store Claims into Cookie #2

Open Engineerumair opened 5 years ago

Engineerumair commented 5 years ago

I need to capture event after successful authentication and store the claims into cookie, can you please guide on this

Engineerumair commented 5 years ago

I have captured the event but I need to store the claims into cookie, can you please let me know how to store it

Here is my code

options.Events = new OpenIdConnectEvents() { OnUserInformationReceived = async n => {

                        // Exchange code for access and ID tokens
                        CookieOptions cookies = new CookieOptions();
                        cookies.Expires = DateTime.Now.AddDays(1);
                        cookies.IsEssential = true;
                        KeyValuePair<string, string> valuePair;
                        // write out the user claims
                        foreach (var claim in n.Principal.Claims)
                        {
                            if (claim.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress")
                            {
                                valuePair = new KeyValuePair<string, string>("emailaddress", claim.Value);
                                n.HttpContext.Request.Cookies.Append(valuePair);         

                            }
                            else if (claim.Type == "name")
                            {
                                valuePair = new KeyValuePair<string, string>(claim.Type, claim.Value);
                                n.HttpContext.Request.Cookies.Append(valuePair);
                            }

                        }
                    }
                    };

            });
damienbod commented 4 years ago

@Engineerumair slow to answer.. sorry.

Have you solved this?

Greetings Damien

Engineerumair commented 4 years ago

Sorry, I was not able to store it