簡體   English   中英

限制登錄時如何向用戶返回正確的錯誤消息

[英]How to return correct error message to User when log in is restricted

我是ASP.Net Web開發的新手,當登錄受到限制時,我只能處理異常。 問題:我具有所有者登錄和租戶登錄。現在,所有者可以限制租戶的登錄。 但是,當登錄受到限制時,我想向租戶顯示他們的憑據正確,但是登錄受到限制。

以下是我迄今為止嘗試過的方法。

調節器

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LoginSubmit(LoginMV userData)
{
    if (userData != null)
    {
        UserMV authenticatedUser = AuthenticateTenant(userData.Username, userData.Password);
        if (authenticatedUser == null)
        { 
              ModelState.AddModelError("Invalid credentials"); 
        }
        else
        {
           //Do the login
        }
    }

    return View("Login", userData);
}

來自API的用戶數據

private static UserMV AuthenticateTenant(string username, string password)
        {
            Res response = mdlAPI.AuthenticateTenant(username, password);
        try{
            response.Validate(username);
           }

        UserMV result = new UserMV()
            {
                DisplayName = response.objTenant.LastName + ", " + response.objTenant.FirstName
            };
            return result;
        }

FYI API響應為response.ResultCode = Restricted

驗證

   public static void Validate(this Res response, string username)
            {
                if (response.ResultCode != Enumerations.AuthResultCode.Successful)
                {
                    string additionalDetails = "";
                    if (response.ResultCode == Enumerations.AuthResultCode.Restricted)
                    {
                        additionalDetails = "Login Restricted.";
                    }
                    throw new ValidationException(additionalDetails);
                }
            }

因此,在上述方法additionalDetails被置為“登錄Restrcited”和ValidationException被調用。 另外,由於引發了異常,因此不會在AuthenticateTenant function創建UserMV對象。

ValidationException

public class ValidationException : Exception
{
    public ValidationException(string message) : base (message)
    { }
}

完成所有這些操作后,代碼序列將返回到控制器。 由於userMV為NULL,因此顯示的錯誤消息為“無效憑據”。 我錯過了什么嗎?

我在ValidationException類中正確獲取了異常。 我是否需要做其他事情才能將異常顯示給用戶。 我是一個初學者,請原諒我的編碼標准。 請指導我要向租戶顯示“其他詳細信息”的其他操作。

查看文件:

@using (Html.BeginForm("LoginSubmit", "Security", FormMethod.Post, new { id = "simLogin" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal-width-inherit">
        @Html.HiddenFor(model => model.ReturnUrl)

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
            @Html.LabelFor(model => model.Username, htmlAttributes: new { @class = "control-label ignoreSpacing col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Username, new { @class = "form-control" } )
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label ignoreSpacing col-md-2" })
            <div class="col-md-10">
                @Html.PasswordFor(model => model.Password, new { @class = "form-control" } )
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Login" class="btn btn-default" />
            </div>
        </div>
    </div>
}

您似乎在依靠AuthenticateTenant()來檢查您的憑據並return驗證結果。

但是,在內部,您正在調用Validate() ,而后者又引發異常。 那個例外改變了代碼的控制流程。 當前,似乎唯一可以捕獲到該錯誤的代碼是您擁有的某些錯誤篩選器,或者已向下傳播到IIS,並且正在顯示通用HTTP 500。

我建議不要在AuthenticateTenant()方法中拋出異常,而應返回null (或者,如果您願意更改方法簽名,則返回false )。

一旦恢復了對流程的控制,就可以依靠返回的結果正確調用ModelState.AddModelError()並帶有適當的消息。

暫無
暫無

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

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