久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

帶有 OpenIdAuthentication 的 ASP.NET:如果未授權,則重

ASP.NET with OpenIdAuthentication: redirect to url if not authorized(帶有 OpenIdAuthentication 的 ASP.NET:如果未授權,則重定向到 url)
本文介紹了帶有 OpenIdAuthentication 的 ASP.NET:如果未授權,則重定向到 url的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試編寫一個使用混合身份驗證方案的 ASP.NET 應用程序.用戶可以將其用戶名和密碼哈希存儲在 UserStore 中,也可以通過 Azure Active Directory 進行身份驗證.

I am attempting to write an ASP.NET application that uses a hybrid authentication scheme. A user can either have his username and password hash stored in the UserStore, or he can authenticate via Azure Active Directory.

我已經創建了如圖所示的登錄表單.它具有標準的 UserNamePassword 輸入,還具有通過 Active Directory 登錄"按鈕.

I have created the login form pictured. It has the standard UserName and Password inputs, but also has a "Login via Active Directory" button.

這很好用.

現在解決問題:應用程序的主頁具有 [Authorize] 屬性.

Now for the problem: The application's home page has the [Authorize] attribute.

public class DefaultController : Controller
{
    [Authorize]
    public ViewResult Index()
    { 
    // Implementation
    }
}

如果用戶沒有登錄,我希望它重定向到頁面Account/Login,讓用戶選擇認證方式.

If the user is not logged in, I want it to redirect to the page Account/Login, allowing the user to choose the authentication method.

IAppBuilder.UseOpenIdConnectAuthentication 添加到管道設置后,它不再重定向到該頁面.相反,它直接進入 Microsoft 登錄頁面.

Once I added IAppBuilder.UseOpenIdConnectAuthentication to the pipeline setup, it no longer redirects to that page. Instead, it goes straight to the Microsoft Login page.

我如何配置它以使 OpenID 身份驗證成為系統的一部分,但允許我指定在用戶未通過身份驗證時如何執行重定向?

How do I configure it so that OpenID authentication is part of the system, but allow me to specify how to perform redirections when the user is not authenticated?

這是我設置管道的代碼:

Here's the code where I set up the pipeline:

appBuilder.SetDefaultSignInAsAuthticationType(CookieAuthenticationDefaults.AuthenticationType_;
var cookieAuthenticationOptions = new CookieAuthenticationOptions
{
     AuthenticationType = DefaultAuthenticationType.ApplicationCookie,
     LoginPath = new Microsoft.Owin.PathString("/Account/Login"),
     Provider = new Security.CookieAuthenticationProvider()
};
appBuilder.UseCookieAuthentication(cookieAuthenticationOptions);
// Now the OpenId authentication
var notificationHandlers = new OpenIdConnectAuthenticationNotificationHandlers 
{
   AuthorizationCodeReceived = async(context) => {
       var jwtSecurityToken = context.JwtSecurityToken;
       // I've written a static method to convert the claims
       // to a user
       var user = await GetOrCreateUser(context.OwinContext, jwtSecurityToken.Claims);
       var signInManager = context.OwinContext.Get<SignInManager>();
       await signInManager.SignInAsync(user, true, false);
   }
}
var openIdOptions = new OpenIdConnectAuthenticationOptions
{
     ClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
     Authority = "https://login.microsoftonline.com/xxxxx.onmicrosoft.com",
     PostLogoutRedirectUri = "https://localhost:52538/Account/Login",
     Notifications = notifcationHandlers
}
appBuilder.UseOpenIdConnectAuthentication(openIdOptions);

當您單擊Active Directory 登錄"時,它會發布到Account/SignInWithOpenId"

When you click "Active Directory Signin", it posts to "Account/SignInWithOpenId"

public ActionResult SignInWithOpenId()
{
    // Send an OpenID Connect sign-in request.
    if (!Request.IsAuthenticated)
    {
        var authenticationProperties = new AuthenticationProperties
        {
            RedirectUri = "/"
        };
        HttpContext.GetOwinContext().Authentication.Challenge
        (
            authenticationProperties,
            OpenIdConnectAuthenticationDefaults.AuthenticationType
        );
        return new EmptyResult();
    }
    else
    {
        return RedirectToAction("Index", "Default");
    }
}

推薦答案

IAppBuilder.UseOpenIdConnectAuthentication(...) 的調用將 Owin 中間件組件放入管道中.當 ASP.NET MVC 返回 401(未授權)的 HttpResponse 時,Owin 中間件組件會檢測到這一點并將其更改為 Http Redirect(代碼 302),并且重定向路徑是 Open Id 提供程序.

The call to IAppBuilder.UseOpenIdConnectAuthentication(...) puts an Owin middleware component in the pipeline. When ASP.NET MVC returns an HttpResponse of 401 (Unauthorized), the Owin Middleware component detects this and changes it to an Http Redirect (code 302), and the redirection path is to the Open Id provider.

但是有一種方法可以解決這個問題:在中間件組件執行重定向之前,它會調用回調 RedirectToIdentityProvider.從這里,您可以覆蓋此重定向.

But there's a way to get around this: before the middleware component performs the redirect, it invokes the callback RedirectToIdentityProvider. From here, you can override this redirection.

這是我的代碼,它會覆蓋重定向,除非它來自請求路徑 Account/SignInWithOpenId.

Here is my code that overrides the redirection unless it is from the request path Account/SignInWithOpenId.

var notificationHandlers = new OpenIdConnectAuthenticationNotifications
{
    AuthorizationCodeReceived = async(context) => {
       // Sign in the user here
    },
    RedirectToIdentityProvider = (context) => {
        if(context.OwinContext.Request.Path.Value != "/Account/SignInWithOpenId")
        {
             context.OwinContext.Response.Redirect("/Account/Login");
             context.HandleResponse();
        }
        return Task.FromResult(0);
    }
}
var openIdOptions = new OpenIdConnectAuthenticationOptions
{
     ClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx",
     Authority = "https://login.microsoftonline.com/xxxxx.onmicrosoft.com",
     PostLogoutRedirectUri = "https://localhost:52538/Account/Login",
     Notifications = notifcationHandlers
}
appBuilder.UseOpenIdConnectAuthentication(openIdOptions);

這篇關于帶有 OpenIdAuthentication 的 ASP.NET:如果未授權,則重定向到 url的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權不起作用)
ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登錄使用角色)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發技
.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 獲取 AAD 訪問令牌以與 Microsoft Graph 一起使用)
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
主站蜘蛛池模板: 久久久久国产 | 精品久久久久久红码专区 | 日本在线观看视频 | 中文字幕一区二区三区日韩精品 | 欧美国产精品久久久 | 日韩av免费在线观看 | 久久精品91久久久久久再现 | 婷婷久久综合 | 91福利网址| 日本三级电影免费观看 | 国产真实精品久久二三区 | 日韩免费视频一区二区 | 毛片一级电影 | 男人天堂视频在线观看 | 国产一区二区免费电影 | 农夫在线精品视频免费观看 | 精品视频一区二区三区 | 国产成人精品久久久 | 免费成人毛片 | 国产精品片aa在线观看 | 日韩中文在线视频 | 久久久久久久久久久国产 | 久久99精品视频 | 免费成人高清在线视频 | av黄色在线观看 | 韩日一区二区三区 | 日韩精品一区二 | 久久大陆| 国产精品久久久久久影院8一贰佰 | 欧美精品在线播放 | 久久这里只有精品首页 | 午夜精品一区二区三区在线视频 | 欧美二区三区 | 国产免费一区二区三区 | 中文字幕一区二区三区四区五区 | 国产区精品在线观看 | 激情av网站 | 亚洲精品中文字幕 | 欧美精品一区二区三区四区 在线 | 男女免费观看在线爽爽爽视频 | 青草青草久热精品视频在线观看 |