2016-07-17 4 views
5

Ich habe ein MVC- und ein Web-API-Projekt, authentifiziert mit ASP.NET MVC Web API-Identität (OWIN-Sicherheit).Verhindern, dass sich Benutzer ohne bestätigte E-Mail anmelden ASP.NET MVC-Web-API-Identität (OWIN-Sicherheit)

Ich habe eine E-Mail Bestätigung an die Register Funktion, die richtig, aber ich bin mir nicht sicher funktioniert, wie wenn emailConfirmed = true vor der Anmeldung zu überprüfen, weil es auf Web-API-Identität nicht eine explizite Anmeldung Funktion ist, es implizite ist.

Ich weiß, dass Microsoft einen guten Grund hat, die Autorisierungsfunktionalität tief einzukapseln, aber gibt es keinen Weg, dies zu erreichen?

Bitte beraten.

Das ist mein Register Funktion:

[AllowAnonymous] 
[Route("Register")] 
public async Task<IHttpActionResult> Register(RegisterBindingModel model) 
    { 
     if (!ModelState.IsValid) 
     { 
      return BadRequest(ModelState); 
     } 

     var user = new ApplicationUser() { UserName = model.Email, Email = model.Email }; 

     IdentityResult result = await UserManager.CreateAsync(user, model.Password); 

     if (!result.Succeeded) 
     { 
      return GetErrorResult(result); 
     } 

     try 
     { 
      var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); 

      var callbackUrl = new Uri(Url.Link("ConfirmEmailRoute", new { userId = user.Id, code = code })); 

      var email = new Email(); 
      email.To = user.Email; 
      email.From = "[email protected]"; 
      email.Subject = "Please confirm your account"; 
      email.Body = "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>"; 

      JsonSerializerSettings settings = new JsonSerializerSettings(); 
      settings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 
      var data = JsonConvert.SerializeObject(email); 

      WebClient client = new WebClient(); 
      client.Headers.Add(HttpRequestHeader.ContentType, "application/json"); 
      var resp = client.UploadString(@"http:...", data); 
     } 
     catch (Exception ex) 
     { 
      throw new Exception(ex.ToString()); 
     } 
     return Ok(); 
    } 
+0

Token-basierte Authentifizierung für Web-API verwenden. Mit dem Token können Sie eine Anfrage mit einem Benutzerkonto verknüpfen. Sobald Sie den Benutzer über Token verifiziert haben, können Sie dann auf "user.emailconfirmed" genauso wie in MVC zugreifen. – Nkosi

+0

Ich habe die Anmeldefunktion, die perfekt mit Token funktioniert, meine Frage ist, wo soll ich die Überprüfung von - wenn 'user.emailconfirmed hinzufügen 'weil es keine Login-Funktion für Identität 2 gibt, ist es implizit. – user3378165

+0

Es sollte Teil Ihrer Login-Funktionalität sein. Andernfalls können Sie es immer in einem Authentifizierungsfiler überprüfen. – Nkosi

Antwort

8

Nach vielen Forschungen ich die Antwort gefunden zu haben.

Ich habe den folgenden Code, wenn die emailconfirmed = true prüft:

var userid = userManager.FindByEmail(context.UserName).Id; 
     if (!userManager.IsEmailConfirmed(userid)) 
     { 
      context.SetError("invalid_grant", "Email registration wasn't confirmed."); 
      return; 
     } 

Zur GrantResourceOwnerCredentials Funktion in der ApplicationOAuthProvider.cs Klasse (unter dem Provider-Ordner).

Dies ist die gesamte Funktion:

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
    { 
     var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>(); 

     ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password); 

     if (user == null) 
     { 
      context.SetError("invalid_grant", "The user name or password is incorrect."); 
      return; 
     } 
     ////Added code here 
     var userid = userManager.FindByEmail(context.UserName).Id; 
     if (!userManager.IsEmailConfirmed(userid)) 
     { 
      context.SetError("invalid_grant", "Email registration wasn't confirmed."); 
      return; 
     } 
     //// 
     ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, 
      OAuthDefaults.AuthenticationType); 
     ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager, 
      CookieAuthenticationDefaults.AuthenticationType); 

     AuthenticationProperties properties = CreateProperties(user.UserName); 
     AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties); 
     context.Validated(ticket); 
     context.Request.Context.Authentication.SignIn(cookiesIdentity); 
    } 

, die perfekt funktioniert und verhindert, dass der Benutzer von der Anmeldung, bevor er die Registrierung E-Mail bestätigt.

+1

Irgendwelche Vorteile zu tun 'var userid = userManager.FindByEmail (context.UserName) .Id;' dann 'if (! UserManager.IsEmailConfirmed (userid))' anstatt einfach 'if (! User.EmailConfirmed) '? –

Verwandte Themen