2016-10-13 4 views
2

Ich entwickle eine Web App mit Asp. Net MVC 5 und diese haben normale ASP.NET-Identität, aber jetzt habe ich eine Mobile App entwickelt und ich brauche Benutzer mit meinem authentifizieren ASP-AppSo aktivieren Sie Client-Authentifizierung von Drittanbietern Asp. NET MVC

Ich bin versucht, eine AJAX Anfrage an meine Login-Methode zu machen, aber die Antwort des Servers eine Ausnahme: ". Validierung des bereitgestellten Fälschungs Token fehlgeschlagen Das Cookie‚__RequestVerificationToken‘und das Formularfeld‚__RequestVerificationToken‘waren vertauscht[ValidateAntiForgeryToken] Dekorateur, und ich denke, dass ASP.NET-Identität hat eine andere Möglichkeit zur Authentifizierung, aber ich weiß es nicht.

Das ist meine Login-Methode:

[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 }); 
} 

Und das ist mein 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

Also, welchen Code hast du? Wie haben Sie ASP.Net Identity konfiguriert? Wie stellst du die Anmeldung ein und was erwartest du zurück? –

+0

@ BrendanGreen danke, ich habe die Frage bearbeitet und meinen Code hinzugefügt –

Antwort

1

Im Allgemeinen wird Ihre MVC-Anwendung zu sprechen ist nur gut für die in einem Browser zu arbeiten. Wenn Sie eine Drittpartei mit Daten versorgen müssen und dies nicht über den Browser geschieht, müssen Sie WebApi verwenden. Und dort können Sie bearer token authentication für Ihre Kunden verwenden.

Verwandte Themen