2016-11-24 46 views
0

这听起来像一个相当基本的问题,但在一般的ASP.NET文件是可怕的,我无法找到如何执行的电子邮件确认。的ASP.NET Web API 2强制要求确认电子邮件

我已经能够建立生成和发送确认电子邮件没有问题,他们工作得很好。

它只是我只希望用户访问“验证”端点被确认。我如何执行此操作。

我能找到的关闭信息似乎只适用于需要将config.SignIn.RequireEmailConfirmation添加到Startup.cs的ASP.NET MVC,但Web API中的Startup.cs完全不同。

作为最后一招,我可以添加:

if (User.EmailConfirmed)到每个端点,但是我觉得这是一个有点戏剧性。

我也尝试添加该“规则”作为自定义身份属性但我不知道如何访问当前用户,因此倾斜检查电子邮件确认。

如何确保电子邮件确认是执行

回答

0

选项:

  1. 当你发送邀请链接给用户使用密码进行注册,确保你正在创建没有密码的用户,再加上从AppUserManager.GeneratePasswordResetTokenAsync向用户发送一个代码的链接(用户.id)用户可以在其中设置密码,并且您可以在此操作中将EmailConfirmed设置为true。

  2. 强制检查用户拥有时生成的令牌电子邮件证实:

在启动

在ConfigureAuth

OAuthOptions = new OAuthAuthorizationServerOptions 
      { 
       TokenEndpointPath = new PathString("/Token"), 
       Provider = new ApplicationOAuthProvider("self"), 
       //... 
       AccessTokenExpireTimeSpan = TimeSpan.FromDays(14), 
       AllowInsecureHttp = false 
      }; 

去检查你的供应商(ApplicationOAuthProvider):

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; 
      } 
      //Insecure message returned, you show to outside world the email exist, 
      //any how here is where you stop the token generation if email of the useris not confirmed, 
      //going forward if you have speciffic role where this validation is not required 
      //check if user is in that role before checking if user has email confirmed 
      if (!user.EmailConfirmed) 
      { 
       context.SetError("invalid_grant", "Email is not confirmed"); 
       return; 
      } 
      //... rest of code 
}