在我的 AccountController 我有以下方法:
/*
* Called when requesting to sign up or sign in
*/
public void SignUpSignIn(string redirectUrl)
{
redirectUrl = redirectUrl ?? "/";
// Use the default policy to process the sign up / sign in flow
HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = redirectUrl });
return;
}
/*
* Called when requesting to sign up
*/
public void SignUp()
{
// Use the default policy to process the sign up flow
HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" }, Globals.SignUpPolicyId);
return;
}
UserFlow 在 Azure 內部設定,稱為B2C_1_signup,這就是Globals.SignUpPolicyId評估的結果。然而,每當我測驗它時,我都會收到HTTP 401 錯誤。
這是創建我的按鈕/鏈接的剃刀代碼:
@Html.ActionLink("Sign Up!", "SignUp", "Account", routeValues: null, htmlAttributes: new { id = "signUpLink", @class = "btn btn-default" })
每當我在 B2C 租戶中測驗 Microsoft 提供的鏈接時,它都會正確顯示注冊頁面。
這是 Microsoft 提供的用于測驗的清理鏈接:
https://mytenantname.b2clogin.com/mytenantname.onmicrosoft.com/oauth2/v2.0/authorize?p=B2C_1_signup&client_id=RANDOM_GUID&nonce=defaultNonce&redirect_uri=http://localhost:1111&scope=openid&response_type=id_token&prompt=login
我錯過了什么??
uj5u.com熱心網友回復:
? 帳戶控制器中定義的重定向 URI 字串應在應用配置設定中定義為私有靜態字串,而 B2C 策略則定義為與公共靜態字串不同的識別符號,因為在用戶流程中,身份驗證重定向將通過參考發生相關的應用程式配置字串,而不是在控制器檔案本身中找到它。由于與瀏覽器會話相關的身份驗證問題,您遇到了 HTTP 401 錯誤。
請在下面找到呼叫 Azure AD B2C 策略的應用程式控制器示例方法,這些方法按以下定義正確作業,用于注冊、登錄和要進行身份驗證的用戶的個人資料:-
public class AccountController : Controller
{
public void SignIn()
{
if (!Request.IsAuthenticated)
{
// To execute a policy, you simply need to trigger an OWIN challenge.
// You can indicate which policy to use by specifying the policy id as the AuthenticationType
HttpContext.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties() { RedirectUri = "/" }, Startup.SignInPolicyId);
}
}
public void SignUp()
{
if (!Request.IsAuthenticated)
{
HttpContext.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties() { RedirectUri = "/" }, Startup.SignUpPolicyId);
}
}
public void Profile()
{
if (Request.IsAuthenticated)
{
HttpContext.GetOwinContext().Authentication.Challenge(
new AuthenticationProperties() { RedirectUri = "/" }, Startup.ProfilePolicyId);
}
}
public void SignOut()
{
// To sign out the user, you should issue an OpenIDConnect sign out request
if (Request.IsAuthenticated)
{
IEnumerable<AuthenticationDescription> authTypes = HttpContext.GetOwinContext().Authentication.GetAuthenticationTypes();
HttpContext.GetOwinContext().Authentication.SignOut(authTypes.Select(t => t.AuthenticationType).ToArray());
}
}
}
另外,請參閱以下鏈接以獲取更詳細的資訊:-

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/311576.html
標籤:C# 。网 asp.net-mvc 拥有 azure-ad-b2c
上一篇:如何防止人們將鏈接粘貼到檔案?
下一篇:訪問一個類的屬性到另一個
