簡體   English   中英

ASP.Net MVC 5-OWIN身份驗證登錄重定向顯示消息

[英]ASP.Net MVC 5 - OWIN Authentication login redirect display message

我在MVC應用程序中使用基於OWIN的身份驗證。 我所有的控制器和操作方法都使用自定義的AuthorizeAttribute進行保護。

當前,如果用戶請求的頁面權限不足(用戶無權訪問,因為他不在有權訪問該功能的組中),他將被重定向到登錄頁面。 此行為可能會使用戶感到困惑,因為他不知道為什么將他重定向到登錄頁面。

在這種情況下可以顯示消息嗎? …OWIN可以向登錄頁面請求添加參數(類似於ReturnUrl參數)嗎?

謝謝你的幫助。

編輯:

[AttributeUsage( AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false )]
public class FunctionAuthorizeAttribute : AuthorizeAttribute
{
    public IUserInformationService UserInformationService { get; set; }
    public IGenericParameterService GenericParameterService { get; set; }
    public String FunctionName { get; set; }

    public FunctionAuthorizeAttribute( String functionName )
    {
        FunctionName = functionName;
    }

    protected override Boolean AuthorizeCore( HttpContextBase httpContext )
    {
        var parameter = GenericParameterService.GetCommonParameters();
        if ( !parameter.CacheSecurityParameter )
            parameter = GenericParameterService.GetCommonParameters(false);

        return !parameter.EnableAuthentication || UserInformationService.HasAccess( FunctionName )
    }
}

使用可以使用會話存儲。 我通常將要在下一個屏幕顯示中顯示的消息存儲在會話變量(作為列表)中。 然后,布局頁面負責顯示適當的消息:

SessionMessage.AddMessage("Huh - you should login first", SessionMessage.MessageType.warning);

在Views / Account / Login.cshtml中添加代碼:

@foreach (SessionEntry item in SessionMessage.GetSessionEntries())
{
   <div class="alert alert-@(Enum.GetName(typeof(SessionMessage.MessageType), item.MessageType).ToLower())">
       <button type="button" class="close" data-dismiss="alert"></button>
       @item.Message
       </div>
}

剩下的一切都在這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM