簡體   English   中英

登錄ASP.Net MVC4

[英]Log in in ASP.Net MVC4

我的應用程序遇到了一些麻煩,因為我需要兩種登錄方式:表單(用戶+密碼)或路由(傳遞用戶唯一標識符)。

我配置了FormsAuthentication,它適用於用戶+密碼方法,但是當我嘗試通過唯一標識符登錄時,無法重定向到我的HomeController。

用戶名和密碼:

    [HttpPost]
    public string Authenticate(string usuario, string senha, string returnUrl)
    {
        var authenticated = this.UsuarioService.Acessar(usuario, senha, out int codUsuario);

        if (authenticated)
        {
            var user = this.UsuarioService.Buscar(codUsuario);

            MontaPermissoes(ref user);

            FormsAuthentication.SetAuthCookie(JsonConvert.SerializeObject(user), false);

            if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                return returnUrl;
            else
                return Url.Action("Index", "Home", new { id = "" });
        }
        return null;
    }

唯一標識符:

    [HttpGet]
    private RedirectToRouteResult Autenticacao(Guid uniqId)
    {
        var usuario = UsuarioService.Buscar(uniqId);

        if (usuario != null)
        {
            MontaPermissoes(ref usuario);

            FormsAuthentication.SetAuthCookie(JsonConvert.SerializeObject(usuario), false);
            return RedirectToAction("Index", "Home", new { culture = RouteData.Values["culture"] });
        }

        return RedirectToAction("Index", "Login", new { culture = RouteData.Values["culture"] });
    }

使用用戶+密碼,我只返回下一個URL來通過Javascript進行重定向,但是唯一的標識符登錄將由另一個使用同一數據庫的應用程序使用。

此問題可能與IIS有關,您的應用程序必須啟用了兩個身份驗證選項。

  • 表單認證
  • 匿名認證

啟用匿名身份驗證后,應用程序可以允許匿名呼叫。

您可以將AllowAnonymous屬性添加到控制器或特定的控制器方法中,以確保不會觸發FormsAuthentication。

[AllowAnonymous]
public class MyController: Controller {}

暫無
暫無

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

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