मैं एक ऐसे प्रोजेक्ट पर काम कर रहा हूं जहां कोई तृतीय पक्ष प्रदाता Oauth2 आधारित प्राधिकरण सर्वर के रूप में कार्य करेगा। एक Asp.net एमवीसी 5 आधारित क्लाइंट जो प्रमाणीकरण (लॉगिन/पासवर्ड का उपयोग करके) प्रमाणीकरण सर्वर को उपयोगकर्ता को भेज देगा और ऑथ सर्वर एमवीसी क्लाइंट को वापस एक्सेस टोकन वापस कर देगा। संसाधन सर्वर (एपीआई) के लिए कोई और कॉल एक्सेस टोकन का उपयोग करके किया जाएगा।ओविन ओपनआईडी कनेक्ट प्रमाणीकरण सुरक्षित नियंत्रक/क्रियाओं को अधिकृत करने में विफल रहता है
इसे प्राप्त करने के लिए मैं Microsoft.Owin.Security.OpenIdConnect और UseOpenIdConnect प्रमाणीकरण एक्सटेंशन का उपयोग कर रहा हूं। मैं सफलतापूर्वक रीडायरेक्ट करने और ऑथ सर्वर से एक्सेस टोकन प्राप्त करने में सक्षम हूं लेकिन क्लाइंट प्रमाणीकरण कुकी नहीं बना रहा है। हर बार जब मैं एक सुरक्षित पृष्ठ तक पहुंचने का प्रयास करता हूं, तो मुझे एक्सेस टोकन वाला कॉलबैक पृष्ठ मिलता है।
मुझे यहां क्या याद आ रही है? मेरा वर्तमान कोड नीचे है।
सुरक्षित नियंत्रक कार्रवाई:
namespace MvcWebApp.Controllers
{
public class SecuredController : Controller
{
// GET: Secured
[Authorize]
public ActionResult Index()
{
return View((User as ClaimsPrincipal).Claims);
}
}
}
स्टार्टअप कक्षा:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType("ClientCookie");
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AuthenticationType = "ClientCookie",
CookieName = CookieAuthenticationDefaults.CookiePrefix + "ClientCookie",
ExpireTimeSpan = TimeSpan.FromMinutes(5)
});
// ***************************************************************************
// Approach 1 : ResponseType = "id_token token"
// ***************************************************************************
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
AuthenticationType = OpenIdConnectAuthenticationDefaults.AuthenticationType,
SignInAsAuthenticationType = app.GetDefaultSignInAsAuthenticationType(),
Authority = "https://thirdparty.com.au/oauth2",
ClientId = "_Th4GVMa0JSrJ8RKcZrzbcexk5ca",
ClientSecret = "a3GVJJbLHkrn9nJRj3IGNvk5eGQa",
RedirectUri = "http://mvcwebapp.local/",
ResponseType = "id_token token",
Scope = "openid",
Configuration = new OpenIdConnectConfiguration
{
AuthorizationEndpoint = "https://thirdparty.com.au/oauth2/authorize",
TokenEndpoint = "https://thirdparty.com.au/oauth2/token",
UserInfoEndpoint = "https://thirdparty.com.au/oauth2/userinfo",
},
Notifications = new OpenIdConnectAuthenticationNotifications
{
SecurityTokenValidated = n =>
{
var token = n.ProtocolMessage.AccessToken;
// persist access token in cookie
if (!string.IsNullOrEmpty(token))
{
n.AuthenticationTicket.Identity.AddClaim(
new Claim("access_token", token));
}
return Task.FromResult(0);
},
AuthenticationFailed = notification =>
{
if (string.Equals(notification.ProtocolMessage.Error, "access_denied", StringComparison.Ordinal))
{
notification.HandleResponse();
notification.Response.Redirect("/");
}
return Task.FromResult<object>(null);
}
}
});
// ***************************************************************************
// Approach 2 : ResponseType = "code"
// ***************************************************************************
//app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
//{
// AuthenticationMode = AuthenticationMode.Active,
// AuthenticationType = OpenIdConnectAuthenticationDefaults.AuthenticationType,
// SignInAsAuthenticationType = app.GetDefaultSignInAsAuthenticationType(),
// Authority = "https://thirdparty.com.au/oauth2",
// ClientId = "_Th4GVMa0JSrJ8RKcZrzbcexk5ca",
// ClientSecret = "a3GVJJbLHkrn9nJRj3IGNvk5eGQa",
// RedirectUri = "http://mvcwebapp.local/",
// ResponseType = "code",
// Scope = "openid",
// Configuration = new OpenIdConnectConfiguration
// {
// AuthorizationEndpoint = "https://thirdparty.com.au/oauth2/authorize",
// TokenEndpoint = "https://thirdparty.com.au/oauth2/token",
// UserInfoEndpoint = "https://thirdparty.com.au/oauth2/userinfo",
// },
// Notifications = new OpenIdConnectAuthenticationNotifications
// {
// AuthorizationCodeReceived = async (notification) =>
// {
// using (var client = new HttpClient())
// {
// var configuration = await notification.Options.ConfigurationManager.GetConfigurationAsync(notification.Request.CallCancelled);
// var request = new HttpRequestMessage(HttpMethod.Get, configuration.TokenEndpoint);
// request.Content = new FormUrlEncodedContent(new Dictionary<string, string>
// {
// {OpenIdConnectParameterNames.ClientId, notification.Options.ClientId},
// {OpenIdConnectParameterNames.ClientSecret, notification.Options.ClientSecret},
// {OpenIdConnectParameterNames.Code, notification.ProtocolMessage.Code},
// {OpenIdConnectParameterNames.GrantType, "authorization_code"},
// {OpenIdConnectParameterNames.ResponseType, "token"},
// {OpenIdConnectParameterNames.RedirectUri, notification.Options.RedirectUri}
// });
// var response = await client.SendAsync(request, notification.Request.CallCancelled);
// response.EnsureSuccessStatusCode();
// var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
// // Add the access token to the returned ClaimsIdentity to make it easier to retrieve.
// notification.AuthenticationTicket.Identity.AddClaim(new Claim(
// type: OpenIdConnectParameterNames.AccessToken,
// value: payload.Value<string>(OpenIdConnectParameterNames.AccessToken)));
// }
// }
// }
//});
}
}
मैंने प्रतिक्रिया_type = "id_token टोकन" की कोशिश की, लेकिन ऐसा लगता है कि मैं जिस पहचान सर्वर का उपयोग कर रहा हूं (WSO2 5.0.0) इसका समर्थन करता है। मुझे पूर्ण ओपनिड कनेक्ट फीचर्स के लिए अगला संस्करण आने तक इंतजार करना होगा। जब मैंने प्रतिक्रिया प्रतिक्रिया_टाइप = "कोड" बदल दिया, तो यह ऑथ प्रक्रिया का पहला भाग पूरा करता है और यूआरएल में एक कोड के साथ लौटाता है लेकिन एक्सेस टोकन प्राप्त करने का अगला अनुरोध नहीं होता है और ओविन पाइपलाइन में कोई पहचान नहीं बनाई जाती है। – TejSoft
दरअसल, यह समर्थित नहीं प्रतीत होता है: http://stackoverflow.com/a/29140396/542757। अफसोस की बात है, आप कोड प्रवाह का उपयोग नहीं कर सकते हैं, क्योंकि ओआईडीसी मिडलवेयर इसका समर्थन नहीं करता है (यह शर्म की बात है और मैंने इसे एएसपी.नेट टीम को कई बार कहा)। एएसपी.NET 5 में माइग्रेट करना एक विकल्प होगा? – Pinpoint
क्या एएसपीनेट 5 पर जा रहा है अगर डब्लूएसओ 2 अभी तक तैयार नहीं है तो सहायक हो जाएगा? आईडी सर्वर एकाधिक साइटों का समर्थन करेगा, कुछ मौजूदा और कुछ नया (बनाया जा रहा है)। नए लोग ASP.net 5 का उपयोग कर सकते हैं लेकिन पुराने लोग 4.5 पर हैं। मैं कुछ और हफ्तों तक इंतजार कर सकता हूं क्योंकि डब्लूएसओ 2 जल्द ही अगले संस्करण को रिलीज़ करेगा। मैंने अपने द्वारा किए गए दो दृष्टिकोणों के साथ प्रश्न अपडेट किया है। – TejSoft