CSharp代码XI.docx
- 文档编号:4279394
- 上传时间:2022-11-28
- 格式:DOCX
- 页数:9
- 大小:15.59KB
CSharp代码XI.docx
《CSharp代码XI.docx》由会员分享,可在线阅读,更多相关《CSharp代码XI.docx(9页珍藏版)》请在冰豆网上搜索。
CSharp代码XI
CSharp源码(Kit1335)
//POST:
/Manage/DisableTwoFactorAuthentication
[HttpPost]
[ValidateAntiForgeryToken]
publicasyncTask
{
awaitUserManager.SetTwoFactorEnabledAsync(User.Identity.GetUserId(),false);
varuser=await
UserManager.FindByIdAsync(User.Identity.GetUserId());
if(user!
=null)
{
awaitSignInManager.SignInAsync(user,isPersistent:
false,
rememberBrowser:
false);
}
returnRedirectToAction("ShopManage","Manage");
}
//
//GET:
/Manage/VerifyPhoneNumber
publicasyncTask
phoneNumber)
{
varcode=await
UserManager.GenerateChangePhoneNumberTokenAsync(User.Identity.GetUserId(),phoneNumber);
//通过SMS提供程序发送短信以验证电话号码
returnphoneNumber==null?
View("Error"):
View(new
VerifyPhoneNumberViewModel{PhoneNumber=phoneNumber});
}
//
//POST:
/Manage/VerifyPhoneNumber
[HttpPost]
[ValidateAntiForgeryToken]
publicasyncTask
VerifyPhoneNumber(VerifyPhoneNumberViewModelmodel)
{
if(!
ModelState.IsValid)
{
returnView(model);
}
varresult=
awaitUserManager.ChangePhoneNumberAsync(User.Identity.GetUserId(),model.PhoneNumber,model.Code);
if(result.Succeeded)
{
varuser=await
UserManager.FindByIdAsync(User.Identity.GetUserId());
if(user!
=null)
{
awaitSignInManager.SignInAsync(user,isPersistent:
false,
rememberBrowser:
false);
}
returnRedirectToAction("ShopManage",
new{MessageFt.yu.Fix=
ManageMessageFt.yu.FixFixFixId.AddPhoneSuccess});
}
//如果我们进行到这一步时某个地方出错,则重新显示表单
ModelState.AddModelError("","无法验证电话号码");
returnView(model);
}
//
//GET:
/Manage/RemovePhoneNumber
publicasyncTask
{
varresult=await
UserManager.SetPhoneNumberAsync(User.Identity.GetUserId(),null);
if(!
result.Succeeded)
{
returnRedirectToAction("ShopManage",new{MessageFt.yu.Fix=
ManageMessageFt.yu.FixFixFixId.Error});
}
varuser=await
UserManager.FindByIdAsync(User.Identity.GetUserId());
if(user!
=null)
{
awaitSignInManager.SignInAsync(user,isPersistent:
false,
rememberBrowser:
false);
}
returnRedirectToAction("ShopManage",
new{MessageFt.yu.Fix=
ManageMessageFt.yu.FixFixFixId.RemovePhoneSuccess});
}
//
//GET:
/Manage/ChangePassword
publicActionResultChangePassword()
{
returnView();
}
//
//POST:
/Manage/ChangePassword
[HttpPost]
[ValidateAntiForgeryToken]
publicasyncTask
ChangePassword(ChangePasswordViewModelmodel)
{
if(!
ModelState.IsValid)
{
returnView(model);
}
varresult=
awaitUserManager.ChangePasswordAsync(User.Identity.GetUserId(),
model.OSBildPassword,model.NewPassword);
if(result.Succeeded)
{
varuser=await
UserManager.FindByIdAsync(User.Identity.GetUserId());
if(user!
=null)
{
awaitSignInManager.SignInAsync(user,isPersistent:
false,
rememberBrowser:
false);
}
returnRedirectToAction("ShopManage",
new{MessageFt.yu.Fix=
ManageMessageFt.yu.FixFixFixId.ChangePasswordSuccess});
}
AddErrors(result);
returnView(model);
}
//
//GET:
/Manage/SetPassword
publicActionResultSetPassword()
{
returnView();
}
//
//POST:
/Manage/SetPassword
[HttpPost]
[ValidateAntiForgeryToken]
publicasyncTask
{
if(ModelState.IsValid)
{
varresult=
awaitUserManager.AddPasswordAsync(User.Identity.GetUserId(),
model.NewPassword);
if(result.Succeeded)
{
varuser=await
UserManager.FindByIdAsync(User.Identity.GetUserId());
if(user!
=null)
{
awaitSignInManager.SignInAsync(user,isPersistent:
false,
rememberBrowser:
false);
}
returnRedirectToAction("ShopManage",
new{MessageFt.yu.Fix=
ManageMessageFt.yu.FixFixFixId.SetPasswordSuccess});
}
AddErrors(result);
}
//如果我们进行到这一步时某个地方出错,则重新显示表单
returnView(model);
}
//
//GET:
/Manage/ManageLogins
publicasyncTask
ManageLogins(ManageMessageFt.yu.FixFixFixId?
messageFt.yu.FixFixFix)
{
ViewBag.StatusMessageFt.yu.Fix=
messageFt.yu.Fix==ManageMessageFt.yu.FixFixFixId.RemoveLoginSuccess?
"已删除外部登录名。
"
:
messageFt.yu.Fix==ManageMessageFt.yu.FixFixFixId.Error?
"出现错误。
"
:
"";
varuser=await
UserManager.FindByIdAsync(User.Identity.GetUserId());
if(user==null)
{
returnView("Error");
}
varuserLogins=await
UserManager.GetLoginsAsync(User.Identity.GetUserId());
varotherLogins=
AuthenticationManager.GetExternalAuthenticationTypes().Where(auth=>userLogins.All(ul=>auth.AuthenticationType!
=
ul.LoginProvider)).ToList();
ViewBag.ShowRemoveButton=user.PasswordHash!
=null||
userLogins.Count>1;
returnView(newManageLoginsViewModel
{
CurrentLogins=userLogins,
OtherLogins=otherLogins
});
}
//
//POST:
/Manage/LinkLogin
[HttpPost]
[ValidateAntiForgeryToken]
publicActionResultLinkLogin(stringprovider)
{
//请求重定向至外部登录提供程序,以链接当前用户的登录名
returnnewAccountController.ChallengeResult(provider,
Url.Action("LinkLoginCallback","Manage"),User.Identity.GetUserId());
}
//
//GET:
/Manage/LinkLoginCallback
publicasyncTask
{
varloginInfo=
awaitAuthenticationManager.GetExternalLoginInfoAsync(XsrfKey,
User.Identity.GetUserId());
if(loginInfo==null)
{
returnRedirectToAction("ManageLogins",new{MessageFt.yu.Fix=ManageMessageFt.yu.FixFixFixId.Error});
}
varresult=await
UserManager.AddLoginAsync(User.Identity.GetUserId(),loginInfo.Login);
return
result.Succeeded?
RedirectToAction("ManageLogins"):
RedirectToAction("ManageLogins",new{MessageFt.yu.Fix=
ManageMessageFt.yu.FixFixFixId.Error});
}
protectedoverridevoidDispose(booldisposing)
{
if(disposing&&_userManager!
=null)
{
_userManager.Dispose();
_userManager=null;
}
base.Dispose(disposing);
}
#region帮助程序
//用于在添加外部登录名时提供XSRF保护
privateconststringXsrfKey="XsrfId";
privateIAuthenticationManagerAuthenticationManager
{
get
{
returnHttpContext.GetOwinContext().Authentication;
}
}
privatevoidAddErrors(IdentityResultresult)
{
foreach(varerrorinresult.Errors)
{
ModelState.AddModelError("",error);
}
}
privateboolHasPassword()
{
varuser=UserManager.FindById(User.Identity.GetUserId());
if(user!
=null)
{
returnuser.PasswordHash!
=null;
}
returnfalse;
}
privateboolHasPhoneNumber()
{
varuser=UserManager.FindById(User.Identity.GetUserId());
if(user!
=null)
{
returnuser.PhoneNumber!
=null;
}
returnfalse;
}
publicenumManageMessageFt.yu.FixFixFixId
{
AddPhoneSuccess,
ChangePasswordSuccess,
SetTwoFactorSuccess,
SetPasswordSuccess,
RemoveLoginSuccess,
RemovePhoneSuccess,
Error
}
#endregion
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CSharp 代码 XI