2016-10-13 49 views
2

我正在用Asp .Net MVC 5開發一個Web應用程序,這有一個正常的ASP.NET身份,但現在我開發了一個移動應用程序,我需要用我的身份驗證用戶ASP應用程序。如何啓用第三方客戶端認證Asp.Net MVC

我試圖讓一個Ajax請求我的登錄方法,但服務器響應異常:「驗證所提供的防僞標記的失敗曲奇‘__RequestVerificationToken’和窗體域‘__RequestVerificationToken’們「因爲我有[ValidateAntiForgeryToken]裝飾,我認爲ASP .NET身份有任何其他方式進行身份驗證,但我不知道。

這是我的登錄方法:

[HttpPost] 
[AllowAnonymous] 
[ValidateAntiForgeryToken] 
public async Task<ActionResult> Login(LoginViewModdel model, string ReturnUrl) 
{ 
    if (ModelState.IsValid) 
    { 
     Employer user = await _employerService.GetByCredentialsAsync(model.Email.Trim(), model.Password); 

     if (user != null) 
     { 
      await SignInAsync(user, model.RememberMe); 
      Response.StatusCode = (int)HttpStatusCode.OK; 
     } 
     else 
     { 
      Employer existingEmail = await _employerService.GetByUsernameAsync(model.Email); 
      if (existingEmail == null) 
      { 
       ModelState.AddModelError("", "El usuario no está registrado. Regístrate o intenta ingresar con un nuevo usuario"); 
       Response.StatusCode = (int)HttpStatusCode.BadRequest; 
       return Json(new { statusCode = 400, message = "El usuario no está registrado. Regístrate o intenta ingresar con un nuevo usuario", Success = "False" }); 
      } 
      else 
      { 
       ModelState.AddModelError("", "Contraseña inválida. Intenta de nuevo"); 
       Response.StatusCode = (int)HttpStatusCode.Unauthorized; 
       return Json(new { statusCode = HttpStatusCode.Unauthorized, Success = "False" }); 
      } 
     } 
    } 
    if (string.IsNullOrWhiteSpace(ReturnUrl)) 
     ReturnUrl = Url.Action("Index", "Home"); 

    return Json(new { statusCode = HttpStatusCode.OK, returnUrl = ReturnUrl }); 
} 

這是我ConfigureAuth:

// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864 
     public void ConfigureAuth(IAppBuilder app) 
     { 

      //Custom provirder create to read language fomr URL 
      CookieAuthenticationProvider provider = new CookieAuthenticationProvider(); 
      var originalHandler = provider.OnApplyRedirect; 
      provider.OnApplyRedirect = context => 
      { 

       var mvcContext = new HttpContextWrapper(HttpContext.Current); 
       var routeData = RouteTable.Routes.GetRouteData(mvcContext); 

       //Get the current language 
       RouteValueDictionary routeValues = new RouteValueDictionary(); 

       //Reuse the RetrunUrl 
       Uri uri = new Uri(context.RedirectUri); 
       string returnUrl = HttpUtility.ParseQueryString(uri.Query)[context.Options.ReturnUrlParameter]; 
       routeValues.Add(context.Options.ReturnUrlParameter, returnUrl); 
       routeValues.Add(Cross.Constants.ModalRouteValue, Cross.Constants.LoginModal); 
       //Overwrite the redirection uri 
       UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext); 
       string NewURI = url.Action("Index", "Home", routeValues); 

       //Overwrite the redirection uri 
       context.RedirectUri = NewURI; 
       originalHandler.Invoke(context); 
      }; 

      // Enable the application to use a cookie to store information for the signed in user 
      app.UseCookieAuthentication(new CookieAuthenticationOptions 
      { 
       AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
       LoginPath = new PathString("/Home/Index?Modal=Login"), 
       Provider = provider, 
      }); 

      // Use a cookie to temporarily store information about a user logging in with a third party login provider 
      app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 

     } 
    } 
+0

那麼,你有什麼代碼?你是如何配置ASP.Net Identity的?你如何提出登錄請求,以及你期待什麼回覆? –

+0

@BrendanGreen謝謝,我編輯了添加我的代碼的問題 –

回答

1

一般來說你的MVC應用程序僅適用於在瀏覽器中工作良好。如果您需要向第三方提供一些數據,而這些數據不是通過瀏覽器發生的,則需要使用WebApi。在那裏您可以使用bearer token authentication爲您的客戶。