asp.net-mvc – 防止没有确认电子邮件的用户登录ASP.NET MVC Web
发布时间:2020-12-16 04:01:58 所属栏目:asp.Net 来源:网络整理
导读:我有一个MVC和一个Web API项目,使用ASP.NET MVC Web API身份(OWIN安全性)进行身份验证. 我向Register函数添加了一个电子邮件确认,该函数正常工作但我不确定如何在登录之前检查emailConfirmed = true是否因为Web API Identity上没有显式的Login函数,它是隐式
我有一个MVC和一个Web API项目,使用ASP.NET MVC Web API身份(OWIN安全性)进行身份验证.
我向Register函数添加了一个电子邮件确认,该函数正常工作但我不确定如何在登录之前检查emailConfirmed = true是否因为Web API Identity上没有显式的Login函数,它是隐式的. 我知道微软有充分的理由深度封装授权功能,但是没有办法实现这一目标吗? 请指教. 这是我的注册功能: [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 = "info@mycompany.com"; 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(); } 解决方法
经过大量的研究,我找到了答案.
我添加了以下代码来检查emailconfirmed = true: var userid = userManager.FindByEmail(context.UserName).Id; if (!userManager.IsEmailConfirmed(userid)) { context.SetError("invalid_grant","Email registration wasn't confirmed."); return; } 到ApplicationOAuthProvider.cs类中的GrantResourceOwnerCredentials函数(在Provider文件夹下). 这是整个功能: 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); } 这完美地运行并且在他确认注册电子邮件之前阻止用户登录. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 在遇到性能问题之前,一个azure实例上有多少用户
- ASP.NET MVC3模型验证DataAnnotations使用客户端验证执行小
- asp.net-mvc – ASP.net MVC 4 WebApi中的嵌套资源
- asp.net-mvc-3 – 调整使用jquery的MVC3应用程序?
- asp.net-mvc – ASP.NET MVC 3绑定确保表单值优先
- asp.net-mvc – 使用ViewData或不使用ViewData
- asp.net – 使用NAnt中的ASP编译器构建ASP .Net MVC应用程序
- asp.net-mvc – 使用Entity FrameWork保存更改/更新数据集中
- asp.net-mvc – 如何在一个视图中使用两个IENumerable模型
- asp.net-mvc – 控制器或模型中的存储库?
推荐文章
站长推荐
- asp.net – 检测Internet Explorer浏览器版本问题
- asp.net-mvc – MVC单元测试控制器的优点
- asp.net – EF代码优先:当IDENTITY_INSERT设置为
- asp.net-mvc-2 – Html.Hidden()插入错误的值
- asp.net-mvc-routing – ASP MVC 5属性路由VS.基
- asp.net-mvc – 在Asp.Net MVC应用程序中使用Str
- asp.net-mvc-2 – 是否可以在EditorFor模板控件中
- 使用.NET Core+Docker 开发微服务
- asp.net – 我可以使用msbuild打包带有发布配置文
- js_html_input中autocomplete="off"在ch
热点阅读