Commit 5fb42a35 authored by yubzy's avatar yubzy

useful git msg

parents
<%@ Page Title="About" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="About.aspx.cs" Inherits="About" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: Title %>.</h2>
<h3>Your application description page.</h3>
<p>Use this area to provide additional information.</p>
</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class About : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
\ No newline at end of file
<%@ Page Title="Log in" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Account_Login" Async="true" %>
<%@ Register Src="~/Account/OpenAuthProviders.ascx" TagPrefix="uc" TagName="OpenAuthProviders" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<h2><%: Title %>.</h2>
<div class="row">
<div class="col-md-8">
<section id="loginForm">
<div class="form-horizontal">
<h4>Use a local account to log in.</h4>
<hr />
<asp:PlaceHolder runat="server" ID="ErrorMessage" Visible="false">
<p class="text-danger">
<asp:Literal runat="server" ID="FailureText" />
</p>
</asp:PlaceHolder>
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="UserName" CssClass="col-md-2 control-label">User name</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="UserName" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="UserName"
CssClass="text-danger" ErrorMessage="The user name field is required." />
</div>
</div>
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="Password" CssClass="col-md-2 control-label">Password</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="Password" TextMode="Password" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="Password" CssClass="text-danger" ErrorMessage="The password field is required." />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<div class="checkbox">
<asp:CheckBox runat="server" ID="RememberMe" />
<asp:Label runat="server" AssociatedControlID="RememberMe">Remember me?</asp:Label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<asp:Button runat="server" OnClick="LogIn" Text="Log in" CssClass="btn btn-default" />
</div>
</div>
</div>
<p>
<asp:HyperLink runat="server" ID="RegisterHyperLink" ViewStateMode="Disabled">Register</asp:HyperLink>
if you don't have a local account.
</p>
</section>
</div>
<div class="col-md-4">
<section id="socialLoginForm">
<uc:openauthproviders runat="server" id="OpenAuthLogin" />
</section>
</div>
</div>
</asp:Content>
using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
using System;
using System.Web;
using System.Web.UI;
using BlockchainInventory;
using System.Web.Security;
public partial class Account_Login : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LogIn(object sender, EventArgs e)
{
if (Membership.ValidateUser(UserName.Text, Password.Text))
{
FormsAuthentication.SetAuthCookie(UserName.Text, true);
Response.Redirect("~/Default.aspx");
}
}
}
\ No newline at end of file
<%@ Page Title="Manage Account" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Manage.aspx.cs" Inherits="Account_Manage" %>
<%@ Register Src="~/Account/OpenAuthProviders.ascx" TagPrefix="uc" TagName="OpenAuthProviders" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: Title %>.</h2>
<div>
<asp:PlaceHolder runat="server" ID="successMessage" Visible="false" ViewStateMode="Disabled">
<p class="text-success"><%: SuccessMessage %></p>
</asp:PlaceHolder>
</div>
<div class="row">
<div class="col-md-12">
<section id="passwordForm">
<asp:PlaceHolder runat="server" ID="setPassword" Visible="false">
<p>
You do not have a local password for this site. Add a local
password so you can log in without an external login.
</p>
<div class="form-horizontal">
<h4>Set Password Form</h4>
<hr />
<asp:ValidationSummary runat="server" ShowModelStateErrors="true" CssClass="text-danger" />
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="password" CssClass="col-md-2 control-label">Password</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="password" TextMode="Password" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="password"
CssClass="text-danger" ErrorMessage="The password field is required."
Display="Dynamic" ValidationGroup="SetPassword" />
<asp:ModelErrorMessage runat="server" ModelStateKey="NewPassword" AssociatedControlID="password"
CssClass="text-danger" SetFocusOnError="true" />
</div>
</div>
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="confirmPassword" CssClass="col-md-2 control-label">Confirm password</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="confirmPassword" TextMode="Password" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="confirmPassword"
CssClass="text-danger" Display="Dynamic" ErrorMessage="The confirm password field is required."
ValidationGroup="SetPassword" />
<asp:CompareValidator runat="server" ControlToCompare="Password" ControlToValidate="confirmPassword"
CssClass="text-danger" Display="Dynamic" ErrorMessage="The password and confirmation password do not match."
ValidationGroup="SetPassword" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<asp:Button runat="server" Text="Set Password" ValidationGroup="SetPassword" OnClick="SetPassword_Click" CssClass="btn btn-default" />
</div>
</div>
</div>
</asp:PlaceHolder>
<asp:PlaceHolder runat="server" ID="changePasswordHolder" Visible="false">
<p>You're logged in as <strong><%: User.Identity.GetUserName() %></strong>.</p>
<div class="form-horizontal">
<h4>Change Password Form</h4>
<asp:ValidationSummary runat="server" ShowModelStateErrors="true" CssClass="text-danger" />
<div class="form-group">
<asp:Label runat="server" ID="CurrentPasswordLabel" AssociatedControlID="CurrentPassword" CssClass="col-md-2 control-label">Current password</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="CurrentPassword" TextMode="Password" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="CurrentPassword"
CssClass="text-danger" ErrorMessage="The current password field is required."
ValidationGroup="ChangePassword" />
</div>
</div>
<div class="form-group">
<asp:Label runat="server" ID="NewPasswordLabel" AssociatedControlID="NewPassword" CssClass="col-md-2 control-label">New password</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="NewPassword" TextMode="Password" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="NewPassword"
CssClass="text-danger" ErrorMessage="The new password is required."
ValidationGroup="ChangePassword" />
</div>
</div>
<div class="form-group">
<asp:Label runat="server" ID="ConfirmNewPasswordLabel" AssociatedControlID="ConfirmNewPassword" CssClass="col-md-2 control-label">Confirm new password</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="ConfirmNewPassword" TextMode="Password" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="ConfirmNewPassword"
CssClass="text-danger" Display="Dynamic" ErrorMessage="Confirm new password is required."
ValidationGroup="ChangePassword" />
<asp:CompareValidator runat="server" ControlToCompare="NewPassword" ControlToValidate="ConfirmNewPassword"
CssClass="text-danger" Display="Dynamic" ErrorMessage="The new password and confirmation password do not match."
ValidationGroup="ChangePassword" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<asp:Button runat="server" Text="Change password" OnClick="ChangePassword_Click" CssClass="btn btn-default" ValidationGroup="ChangePassword" />
</div>
</div>
</div>
</asp:PlaceHolder>
</section>
<section id="externalLoginsForm">
<asp:ListView runat="server"
ItemType="Microsoft.AspNet.Identity.UserLoginInfo"
SelectMethod="GetLogins" DeleteMethod="RemoveLogin" DataKeyNames="LoginProvider,ProviderKey">
<LayoutTemplate>
<h4>Registered Logins</h4>
<table class="table">
<tbody>
<tr runat="server" id="itemPlaceholder"></tr>
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%#: Item.LoginProvider %></td>
<td>
<asp:Button runat="server" Text="Remove" CommandName="Delete" CausesValidation="false"
ToolTip='<%# "Remove this " + Item.LoginProvider + " login from your account" %>'
Visible="<%# CanRemoveExternalLogins %>" CssClass="btn btn-default" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<uc:openauthproviders runat="server" returnurl="~/Account/Manage" />
</section>
</div>
</div>
</asp:Content>
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using BlockchainInventory;
public partial class Account_Manage : System.Web.UI.Page
{
protected string SuccessMessage
{
get;
private set;
}
protected bool CanRemoveExternalLogins
{
get;
private set;
}
private bool HasPassword(UserManager manager)
{
var user = manager.FindById(User.Identity.GetUserId());
return (user != null && user.PasswordHash != null);
}
protected void Page_Load()
{
if (!IsPostBack)
{
// Determine the sections to render
UserManager manager = new UserManager();
if (HasPassword(manager))
{
changePasswordHolder.Visible = true;
}
else
{
setPassword.Visible = true;
changePasswordHolder.Visible = false;
}
CanRemoveExternalLogins = manager.GetLogins(User.Identity.GetUserId()).Count() > 1;
// Render success message
var message = Request.QueryString["m"];
if (message != null)
{
// Strip the query string from action
Form.Action = ResolveUrl("~/Account/Manage");
SuccessMessage =
message == "ChangePwdSuccess" ? "Your password has been changed."
: message == "SetPwdSuccess" ? "Your password has been set."
: message == "RemoveLoginSuccess" ? "The account was removed."
: String.Empty;
successMessage.Visible = !String.IsNullOrEmpty(SuccessMessage);
}
}
}
protected void ChangePassword_Click(object sender, EventArgs e)
{
if (IsValid)
{
UserManager manager = new UserManager();
IdentityResult result = manager.ChangePassword(User.Identity.GetUserId(), CurrentPassword.Text, NewPassword.Text);
if (result.Succeeded)
{
var user = manager.FindById(User.Identity.GetUserId());
IdentityHelper.SignIn(manager, user, isPersistent: false);
Response.Redirect("~/Account/Manage?m=ChangePwdSuccess");
}
else
{
AddErrors(result);
}
}
}
protected void SetPassword_Click(object sender, EventArgs e)
{
if (IsValid)
{
// Create the local login info and link the local account to the user
UserManager manager = new UserManager();
IdentityResult result = manager.AddPassword(User.Identity.GetUserId(), password.Text);
if (result.Succeeded)
{
Response.Redirect("~/Account/Manage?m=SetPwdSuccess");
}
else
{
AddErrors(result);
}
}
}
public IEnumerable<UserLoginInfo> GetLogins()
{
UserManager manager = new UserManager();
var accounts = manager.GetLogins(User.Identity.GetUserId());
CanRemoveExternalLogins = accounts.Count() > 1 || HasPassword(manager);
return accounts;
}
public void RemoveLogin(string loginProvider, string providerKey)
{
UserManager manager = new UserManager();
var result = manager.RemoveLogin(User.Identity.GetUserId(), new UserLoginInfo(loginProvider, providerKey));
string msg = String.Empty;
if (result.Succeeded)
{
var user = manager.FindById(User.Identity.GetUserId());
IdentityHelper.SignIn(manager, user, isPersistent: false);
msg = "?m=RemoveLoginSuccess";
}
Response.Redirect("~/Account/Manage" + msg);
}
private void AddErrors(IdentityResult result)
{
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error);
}
}
}
\ No newline at end of file
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="OpenAuthProviders.ascx.cs" Inherits="OpenAuthProviders" %>
<div id="socialLoginList">
<h4>Use another service to log in.</h4>
<hr />
<asp:ListView runat="server" ID="providerDetails" ItemType="System.String"
SelectMethod="GetProviderNames" ViewStateMode="Disabled">
<ItemTemplate>
<p>
<button type="submit" class="btn btn-default" name="provider" value="<%#: Item %>"
title="Log in using your <%#: Item %> account.">
<%#: Item %>
</button>
</p>
</ItemTemplate>
<EmptyDataTemplate>
<div>
<p>There are no external authentication services configured. See <a href="http://go.microsoft.com/fwlink/?LinkId=252803">this article</a> for details on setting up this ASP.NET application to support logging in via external services.</p>
</div>
</EmptyDataTemplate>
</asp:ListView>
</div>
\ No newline at end of file
using Microsoft.Owin.Security;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using BlockchainInventory;
public partial class OpenAuthProviders : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
var provider = Request.Form["provider"];
if (provider == null)
{
return;
}
// Request a redirect to the external login provider
string redirectUrl = ResolveUrl(String.Format(CultureInfo.InvariantCulture, "~/Account/RegisterExternalLogin?{0}={1}&returnUrl={2}", IdentityHelper.ProviderNameKey, provider, ReturnUrl));
var properties = new AuthenticationProperties() { RedirectUri = redirectUrl };
// Add xsrf verification when linking accounts
if (Context.User.Identity.IsAuthenticated)
{
properties.Dictionary[IdentityHelper.XsrfKey] = Context.User.Identity.GetUserId();
}
Context.GetOwinContext().Authentication.Challenge(properties, provider);
Response.StatusCode = 401;
Response.End();
}
}
public string ReturnUrl { get; set; }
public IEnumerable<string> GetProviderNames()
{
return Context.GetOwinContext().Authentication.GetExternalAuthenticationTypes().Select(t => t.AuthenticationType);
}
}
\ No newline at end of file
<%@ Page Title="Register" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Register.aspx.cs" Inherits="Account_Register" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<h2><%: Title %>.</h2>
<p class="text-danger">
<asp:Literal runat="server" ID="ErrorMessage" />
</p>
<div class="form-horizontal">
<h4>Create a new account.</h4>
<hr />
<asp:ValidationSummary runat="server" CssClass="text-danger" />
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="UserName" CssClass="col-md-2 control-label">User name</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="UserName" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="UserName"
CssClass="text-danger" ErrorMessage="The user name field is required." />
</div>
</div>
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="Password" CssClass="col-md-2 control-label">Password</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="Password" TextMode="Password" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="Password"
CssClass="text-danger" ErrorMessage="The password field is required." />
</div>
</div>
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="ConfirmPassword" CssClass="col-md-2 control-label">Confirm password</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="ConfirmPassword" TextMode="Password" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="ConfirmPassword"
CssClass="text-danger" Display="Dynamic" ErrorMessage="The confirm password field is required." />
<asp:CompareValidator runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword"
CssClass="text-danger" Display="Dynamic" ErrorMessage="The password and confirmation password do not match." />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<asp:Button runat="server" OnClick="CreateUser_Click" Text="Register" CssClass="btn btn-default" />
</div>
</div>
</div>
</asp:Content>
using Microsoft.AspNet.Identity;
using System;
using System.Linq;
using System.Web.UI;
using BlockchainInventory;
using System.Web.Security;
public partial class Account_Register : Page
{
protected void CreateUser_Click(object sender, EventArgs e)
{
}
}
\ No newline at end of file
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="RegisterExternalLogin.aspx.cs" Inherits="Account_RegisterExternalLogin" Async="true" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<h3>Register with your <%: ProviderName %> account</h3>
<asp:PlaceHolder runat="server">
<div class="form-horizontal">
<h4>Association Form</h4>
<hr />
<asp:ValidationSummary runat="server" ShowModelStateErrors="true" CssClass="text-danger" />
<p class="text-info">
You've authenticated with <strong><%: ProviderName %></strong>. Please enter a user name below for the current site
and click the Log in button.
</p>
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="userName" CssClass="col-md-2 control-label">User name</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="userName" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="userName"
Display="Dynamic" CssClass="text-danger" ErrorMessage="User name is required" />
<asp:ModelErrorMessage runat="server" ModelStateKey="UserName" CssClass="text-danger" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<asp:Button runat="server" Text="Log in" CssClass="btn btn-default" OnClick="LogIn_Click" />
</div>
</div>
</div>
</asp:PlaceHolder>
</asp:Content>
using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
using System;
using System.Web;
using BlockchainInventory;
public partial class Account_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; }
}
protected void Page_Load()
{
// Process the result from an auth provider in the request
ProviderName = IdentityHelper.GetProviderNameFromRequest(Request);
if (String.IsNullOrEmpty(ProviderName))
{
Response.Redirect("~/Account/Login");
}
if (!IsPostBack)
{
var manager = new UserManager();
var loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();
if (loginInfo == null)
{
Response.Redirect("~/Account/Login");
}
var user = manager.Find(loginInfo.Login);
if (user != null)
{
IdentityHelper.SignIn(manager, user, isPersistent: false);
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else if (User.Identity.IsAuthenticated)
{
// Apply Xsrf check when linking
var verifiedloginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo(IdentityHelper.XsrfKey, User.Identity.GetUserId());
if (verifiedloginInfo == null)
{
Response.Redirect("~/Account/Login");
}
var result = manager.AddLogin(User.Identity.GetUserId(), verifiedloginInfo.Login);
if (result.Succeeded)
{
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
AddErrors(result);
return;
}
}
else
{
userName.Text = loginInfo.DefaultUserName;
}
}
}
protected void LogIn_Click(object sender, EventArgs e)
{
CreateAndLoginUser();
}
private void CreateAndLoginUser()
{
if (!IsValid)
{
return;
}
var manager = new UserManager();
var user = new ApplicationUser() { UserName = userName.Text };
IdentityResult result = manager.Create(user);
if (result.Succeeded)
{
var loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();
if (loginInfo == null)
{
Response.Redirect("~/Account/Login");
return;
}
result = manager.AddLogin(user.Id, loginInfo.Login);
if (result.Succeeded)
{
IdentityHelper.SignIn(manager, user, isPersistent: false);