Update 07082021
This commit is contained in:
130
WebFormApp/Account/RegisterExternalLogin.aspx.cs
Normal file
130
WebFormApp/Account/RegisterExternalLogin.aspx.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
using System;
|
||||
using System.Web;
|
||||
using Microsoft.AspNet.Identity;
|
||||
using Microsoft.AspNet.Identity.Owin;
|
||||
using Microsoft.Owin.Security;
|
||||
using Owin;
|
||||
using WebFormApp.Models;
|
||||
|
||||
namespace WebFormApp.Account
|
||||
{
|
||||
public partial class RegisterExternalLogin : System.Web.UI.Page
|
||||
{
|
||||
protected string ProviderName
|
||||
{
|
||||
get { return (string)ViewState["ProviderName"] ?? String.Empty; }
|
||||
private set { ViewState["ProviderName"] = value; }
|
||||
}
|
||||
|
||||
protected string ProviderAccountKey
|
||||
{
|
||||
get { return (string)ViewState["ProviderAccountKey"] ?? String.Empty; }
|
||||
private set { ViewState["ProviderAccountKey"] = value; }
|
||||
}
|
||||
|
||||
private void RedirectOnFail()
|
||||
{
|
||||
Response.Redirect((User.Identity.IsAuthenticated) ? "~/Account/Manage" : "~/Account/Login");
|
||||
}
|
||||
|
||||
protected void Page_Load()
|
||||
{
|
||||
// Ergebnis von einem Authentifizierungsanbieter in der Anforderung verarbeiten
|
||||
ProviderName = IdentityHelper.GetProviderNameFromRequest(Request);
|
||||
if (String.IsNullOrEmpty(ProviderName))
|
||||
{
|
||||
RedirectOnFail();
|
||||
return;
|
||||
}
|
||||
if (!IsPostBack)
|
||||
{
|
||||
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
|
||||
var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
|
||||
var loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();
|
||||
if (loginInfo == null)
|
||||
{
|
||||
RedirectOnFail();
|
||||
return;
|
||||
}
|
||||
var user = manager.Find(loginInfo.Login);
|
||||
if (user != null)
|
||||
{
|
||||
signInManager.SignIn(user, isPersistent: false, rememberBrowser: false);
|
||||
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
|
||||
}
|
||||
else if (User.Identity.IsAuthenticated)
|
||||
{
|
||||
// XSRF-Überprüfung beim Verknüpfen anwenden
|
||||
var verifiedloginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo(IdentityHelper.XsrfKey, User.Identity.GetUserId());
|
||||
if (verifiedloginInfo == null)
|
||||
{
|
||||
RedirectOnFail();
|
||||
return;
|
||||
}
|
||||
|
||||
var result = manager.AddLogin(User.Identity.GetUserId(), verifiedloginInfo.Login);
|
||||
if (result.Succeeded)
|
||||
{
|
||||
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
|
||||
}
|
||||
else
|
||||
{
|
||||
AddErrors(result);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
email.Text = loginInfo.Email;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void LogIn_Click(object sender, EventArgs e)
|
||||
{
|
||||
CreateAndLoginUser();
|
||||
}
|
||||
|
||||
private void CreateAndLoginUser()
|
||||
{
|
||||
if (!IsValid)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
|
||||
var signInManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();
|
||||
var user = new ApplicationUser() { UserName = email.Text, Email = email.Text };
|
||||
IdentityResult result = manager.Create(user);
|
||||
if (result.Succeeded)
|
||||
{
|
||||
var loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();
|
||||
if (loginInfo == null)
|
||||
{
|
||||
RedirectOnFail();
|
||||
return;
|
||||
}
|
||||
result = manager.AddLogin(user.Id, loginInfo.Login);
|
||||
if (result.Succeeded)
|
||||
{
|
||||
signInManager.SignIn(user, isPersistent: false, rememberBrowser: false);
|
||||
|
||||
// Weitere Informationen zum Aktivieren der Kontobestätigung und Kennwortzurücksetzung finden Sie unter https://go.microsoft.com/fwlink/?LinkID=320771
|
||||
// var code = manager.GenerateEmailConfirmationToken(user.Id);
|
||||
// Diesen Link per E-Mail senden: IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id)
|
||||
|
||||
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
|
||||
return;
|
||||
}
|
||||
}
|
||||
AddErrors(result);
|
||||
}
|
||||
|
||||
private void AddErrors(IdentityResult result)
|
||||
{
|
||||
foreach (var error in result.Errors)
|
||||
{
|
||||
ModelState.AddModelError("", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user