मैं Startup.Auth.cs में ऐसा करके एक तरह से सफल रहा हूँ दायरक्या एक एएसपी.नेट एमवीसी अनुप्रयोग में एज़ूर एडी और व्यक्तिगत खाता प्रमाणीकरण दोनों संभव है?
// Configure the db context and user manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.Properties["Microsoft.Owin.Security.Constants.DefaultSignInAsAuthenticationType"] = "ExternalCookie";
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri
});
चुनौती मैं है, जब कोई उपयोगकर्ता साइन आउट रहता है, और एक गैर प्रवेश पृष्ठ हिट करने के लिए कोशिश करता है की तरह कहते हैं कि http://mywebsite/users/management है http://mywebsite/account/login के बजाय एप्लिकेशन स्वचालित रूप से Azure AD साइन-इन पृष्ठ पर रीडायरेक्ट करता है, जो सही नहीं है। क्योंकि ऐसे उपयोगकर्ता हो सकते हैं जिनके पास Azure AD पर खाता नहीं है। यहां तक कि यदि हम एडी साइन इन पेज में उचित उपयोगकर्ता आईडी और पासवर्ड देते हैं और साइन-इन पर क्लिक करते हैं, तो यह http://login.windows.net के भीतर विभिन्न यूआरएल के बीच रीडायरेक्ट रखता है और हमारी वेबसाइट पर कभी भी नहीं जाता है। मुझे यकीन है कि मैं गलत यहाँ क्या कर रहा हूँ नहीं कर रहा हूँ
AuthenticationManager.SignOut(new string[] { DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.ApplicationCookie, OpenIdConnectAuthenticationDefaults.AuthenticationType });
return RedirectToAction("Login", "Account");
-
यहाँ लॉगआउट कोड है।
संपादित करें 1 मेरे ExternalLoginCallback विधि
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
return RedirectToAction("Login");
}
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Sid, "Office365"));
// Sign in the user with this external login provider if the user already has a login
var user = await UserManager.FindByEmailAsync(loginInfo.ExternalIdentity.Name);
if (user != null && user.IsActive == true && user.EmailConfirmed == true)
{
var result = await UserManager.AddLoginAsync(user.Id, loginInfo.Login);
if (result.Succeeded)
{
if (claims != null)
{
var userIdentity = await user.GenerateUserIdentityAsync(UserManager);
userIdentity.AddClaims(claims);
}
}
await SignInAsync(user, isPersistent: true);
Session[AppConstants.General.UserID] = user.Id;
string fullName = string.Format("{0} {1}",user.FirstName,user.LastName);
Session[AppConstants.General.UserFullName] = fullName;
return RedirectToLocal(returnUrl);
}
else
{
// If the user does not have an account, tell that to the user.
ViewBag.ReturnUrl = returnUrl;
ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { Email = loginInfo.Email });
}
}
मैं इस कोशिश की, लेकिन मेरे लिए चर "मूल्य" जब में एक बाहरी उपयोगकर्ता के लॉग रिक्त है। यह मेरे दूसरे app.UseCookieAuthentication() सेटिंग के कारण है? असल में, मैंने अपने ऐप को बदल दिया। OpenIdConnectAuthentication() का उपयोग करें और बाकी के साथ रखें। क्या यह सही है? –
आप सही हैं। Answere में EDIT 2 देखें। यदि आप –
चाहते हैं तो आप निश्चित रूप से बाहरी लॉजिक कॉलबैक फ़ंक्शन को सरल बना सकते हैं, लेकिन मुझे इस लाइन पर शून्य मिलती है - 'कोड' var value = ci.FindFirstValue (System.Security.Claims.ClaimTypes.Sid); –