سلام
من یک مسئله رو در Extenral Login با استفاده از Open Id connect در mvc رو متوجه نمی شم. (همون که OWIN security components استفاده می کنه)
ببینید من در متد کانفیگ در استارت آپ با استفاده از این صفحه https://devblogs.microsoft.com/aspnet/owin-security-components-in-asp-net-openid-connect/ و بسیاری از مقالات دیگه ای که توی نت هست، درستش کردم
وقتی که کاربر رو به سمت سرور Aithentication می فرستم و کاربر به درستی تأیید هویت می شه و برمی گرده به متد CallBack که قبلاً در استارت آپ مشخص کردم. مطابق دستور العمل یک کد بهم بر می گرده (البته چون من از نوع Authorization Code توی Opetion استفاده کرده بودم) نمونه کد من توی اینترنت فراوونه کدم رو به ضمیمه ارسال می کنم
public class Startup { public void Configuration(IAppBuilder app) { // Configure Auth0 parameters string auth0Domain = ConfigurationManager.AppSettings["auth0:Domain"]; string auth0ClientId = ConfigurationManager.AppSettings["auth0:ClientId"]; string auth0ClientSecret = ConfigurationManager.AppSettings["auth0:ClientSecret"]; string auth0RedirectUri = ConfigurationManager.AppSettings["auth0:RedirectUri"]; string auth0PostLogoutRedirectUri = ConfigurationManager.AppSettings["auth0:PostLogoutRedirectUri"]; // Enable Kentor Cookie Saver middleware app.UseKentorOwinCookieSaver(); // Set Cookies as default authentication type app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = CookieAuthenticationDefaults.AuthenticationType }); // Configure Auth0 authentication app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions { AuthenticationType = "Auth0", Authority = $"https://{auth0Domain}", ClientId = auth0ClientId, ClientSecret = auth0ClientSecret, RedirectUri = auth0RedirectUri, PostLogoutRedirectUri = auth0PostLogoutRedirectUri, ResponseType = OpenIdConnectResponseType.Code, Scope = "openid", TokenValidationParameters = new TokenValidationParameters { NameClaimType = "name" }, Notifications = new OpenIdConnectAuthenticationNotifications { RedirectToIdentityProvider = notification => { if (notification.ProtocolMessage.RequestType == OpenIdConnectRequestType.Logout) { var logoutUri = $"https://{auth0Domain}/v2/logout?client_id={auth0ClientId}"; var postLogoutUri = notification.ProtocolMessage.PostLogoutRedirectUri; if (!string.IsNullOrEmpty(postLogoutUri)) { if (postLogoutUri.StartsWith("/")) { // transform to absolute var request = notification.Request; postLogoutUri = request.Scheme + "://" + request.Host + request.PathBase + postLogoutUri; } logoutUri += $"&returnTo={ Uri.EscapeDataString(postLogoutUri)}"; } notification.Response.Redirect(logoutUri); notification.HandleResponse(); } return Task.FromResult(0); }, MessageReceived = OnMessageRecived, SecurityTokenReceived = OnSecurityTokenRecied, AuthenticationFailed = OnAuthenticationFailed, AuthorizationCodeReceived = async n => { // Exchange code for access and ID tokens var tokenClient = new TokenClient($"{auth0Domain}/v1/token", auth0ClientId, auth0ClientSecret); var tokenResponse = await tokenClient.RequestAuthorizationCodeAsync(n.Code, auth0RedirectUri); if (tokenResponse.IsError) { throw new Exception(tokenResponse.Error); } var userInfoClient = new UserInfoClient($"{auth0Domain}/v1/userinfo"); var userInfoResponse = await userInfoClient.GetAsync(tokenResponse.AccessToken); var claims = new List<Claim>(userInfoResponse.Claims) { new Claim("id_token", tokenResponse.IdentityToken), new Claim("access_token", tokenResponse.AccessToken) }; n.AuthenticationTicket.Identity.AddClaims(claims); }, SecurityTokenValidated = context => { // add Auth0 Access Token as claim var accessToken = context.ProtocolMessage.AccessToken; if (!string.IsNullOrEmpty(accessToken)) { context.AuthenticationTicket.Identity.AddClaim(new Claim("access_token", accessToken)); } return Task.FromResult(0); } } }); } private Task OnSecurityTokenVarified(SecurityTokenValidatedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> arg) { var a = arg; return Task.FromResult(0); } private Task OnSecurityTokenRecied(SecurityTokenReceivedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> arg) { var a = arg; return Task.FromResult(0); } private Task OnMessageRecived(MessageReceivedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> arg) { var a = arg; return Task.FromResult(0); } private async Task OnAuthorizationCodeReceivedAsync(AuthorizationCodeReceivedNotification context) { var claimsPrincipal = new ClaimsPrincipal(context.AuthenticationTicket.Identity); // Upon successful sign in, get the access token & cache it using MSAL //IConfidentialClientApplication clientApp = MSALAppBuilder.BuildConfidentialClientApplication(claimsPrincipal); //AuthenticationResult result = await clientApp // .AcquireTokenByAuthorizationCode(VeracityIntegrationOptions.DefaultScope.Split(' '), context.Code) // .ExecuteAsync(); } /// <summary> /// Called when authentication fails. /// </summary> /// <param name="notification">The notification.</param> /// <returns></returns> private Task OnAuthenticationFailed(AuthenticationFailedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification) { notification.HandleResponse(); notification.Response.Redirect("/Error?message=" + notification.Exception.Message); return Task.FromResult(0); } }
حالا یک سوال
الان برای اینکه من توکن رو دریافت کنم که فرضاً بتونم نام کاربر و یا ایمیل کاربرم رو از سرور Aithentication بگیرم باید یک درخواست دیگه به سرور بزنم؟ من جایی رو اشتباه می کنم؟ برا سروری مثل https://auth0.com/ که کارش همینه هیچ جایی ننوشته که بعدش چی میشه. همه فقط توی کال بک از AuthenticationManager.GetExternalLoginInfo استفاده می کنن همین! مال من مقدارش ناله!
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)