本文整理汇总了C#中Windows.Security.Credentials.PasswordVault.Add方法的典型用法代码示例。如果您正苦于以下问题:C# PasswordVault.Add方法的具体用法?C# PasswordVault.Add怎么用?C# PasswordVault.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Windows.Security.Credentials.PasswordVault
的用法示例。
在下文中一共展示了PasswordVault.Add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ButtonBase_OnClick
//Use your consumerKey and ConsumerSecret
private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(pinText.Text))
{
var msgDialog = new MessageDialog("Please Enter the pin showed below");
await msgDialog.ShowAsync();
}
else
{
var pinCode = pinText.Text.Trim();
var userCredentials = AuthFlow.CreateCredentialsFromVerifierCode(pinCode, authenticationContext);
Auth.SetCredentials(userCredentials);
var vault = new PasswordVault();
vault.Add(new PasswordCredential("Friend", "TwitterAccessToken", userCredentials.AccessToken));
vault.Add(new PasswordCredential("Friend", "TwitterAccessTokenSecret", userCredentials.AccessTokenSecret));
var localSettings = ApplicationData.Current.LocalSettings;
var frame = Window.Current.Content as Frame;
if (localSettings.Values.ContainsKey("FirstTimeRunComplete"))
{
frame?.Navigate(typeof(MainPage));
}
else
{
frame?.Navigate(typeof(FirstTimeTutorial));
}
}
}
示例2: AuthenticateAsync
/// <summary>
/// Starts the authentication process.
/// </summary>
/// <param name="provider">The provider to authenticate with.</param>
public async Task AuthenticateAsync(MobileServiceAuthenticationProvider provider)
{
try
{
var vault = new PasswordVault();
// Login with the identity provider.
var user = await AzureAppService.Current
.LoginAsync(provider);
// Create and store the user credentials.
var credential = new PasswordCredential(provider.ToString(),
user.UserId, user.MobileServiceAuthenticationToken);
vault.Add(credential);
}
catch (InvalidOperationException invalidOperationException)
{
if (invalidOperationException.Message
.Contains("Authentication was cancelled by the user."))
{
throw new AuthenticationCanceledException("Authentication canceled by user",
invalidOperationException);
}
throw new AuthenticationException("Authentication failed", invalidOperationException);
}
catch (Exception e)
{
throw new AuthenticationException("Authentication failed", e);
}
}
示例3: Login
/// <summary>
/// Attempt to sign in to GitHub using the credentials supplied.
/// </summary>
/// <param name="username">GitHub username</param>
/// <param name="password">GitHub password</param>
/// <param name="cached">Whether these credentials came from local storage</param>
/// <returns>true if successful, false otherwise</returns>
public async Task<string> Login(string username, string password, bool cached = false)
{
client.Credentials = new Credentials(username, password);
try
{
//hacky way of determining whether the creds are correct
await client.GitDatabase.Reference.Get("dhbrett", "Graffiti", "heads/master");
//we haven't thrown so all good
SignedIn = true;
//these are new credentials, save them
if (!cached)
{
var pv = new PasswordVault();
pv.Add(new PasswordCredential { Resource = RESOURCE, UserName = username, Password = password });
localSettings.Values[USERNAME] = username;
}
return "pass";
}
catch(Exception e)
{
if (e.Message.Contains("two-factor"))
{
return "tfa";
}
else if (cached)
{
var pv = new PasswordVault();
pv.Remove(pv.Retrieve(RESOURCE, username));
}
return "fail";
}
}
示例4: Save_Click
private void Save_Click(object sender, RoutedEventArgs e)
{
String result = "";
if (InputResourceValue.Text == "" || InputUserNameValue.Text == "" || InputPasswordValue.Password == "")
{
rootPage.NotifyUser("Inputs are missing. Resource, User name and Password are required", NotifyType.ErrorMessage);
}
else
{
try
{
//Add a credential to PasswordVault by supplying resource, username, and password
PasswordVault vault = new PasswordVault();
PasswordCredential cred = new PasswordCredential(InputResourceValue.Text, InputUserNameValue.Text, InputPasswordValue.Password);
vault.Add(cred);
//Output credential added to debug spew
rootPage.NotifyUser("Credential saved successfully. " + "Resource: " + cred.Resource.ToString() + " Username: " + cred.UserName.ToString() + " Password: " + cred.Password.ToString() + ".", NotifyType.StatusMessage);
}
catch (Exception Error) // No stored credentials, so none to delete
{
rootPage.NotifyUser(Error.Message, NotifyType.ErrorMessage);
}
}
}
开发者ID:t-angma,项目名称:Windows-universal-samples,代码行数:26,代码来源:scenario1_addreadremovecredentials.xaml.cs
示例5: Save
public void Save(string userName, string password)
{
Clear();
PasswordVault vault = new PasswordVault();
vault.Add(new PasswordCredential(AppResourceName, userName, password));
}
示例6: SaveCredentialToLocker
public static void SaveCredentialToLocker(MobileCredentials mobileCredentials)
{
// clean up
var vault = new PasswordVault();
try
{
var credentialList = vault.FindAllByResource(ResourceName);
foreach (var passwordCredential in credentialList)
{
vault.Remove(passwordCredential);
}
}
// ReSharper disable once EmptyGeneralCatchClause
catch (Exception)
{
}
var credential = new PasswordCredential
{
Resource = ResourceName,
UserName = mobileCredentials.MobileNumber,
Password = mobileCredentials.Password
};
vault.Add(credential);
}
示例7: SaveCredential
public static void SaveCredential(string userName, string password)
{
PasswordVault passwordVault = new PasswordVault();
PasswordCredential passwordCredential = new PasswordCredential(RESOURCE, userName, password);
passwordVault.Add(passwordCredential);
}
示例8: AuthenticateAsync
// Log the user in with specified provider (Microsoft Account or Facebook)
private async Task AuthenticateAsync()
{
// Use the PasswordVault to securely store and access credentials.
PasswordVault vault = new PasswordVault();
PasswordCredential credential = null;
try
{
// Try to get an existing credential from the vault.
credential = vault.FindAllByResource(provider).FirstOrDefault();
}
catch (Exception)
{
// do nothing
}
if (credential != null)
{
// Create a user from the stored credentials.
user = new MobileServiceUser(credential.UserName);
credential.RetrievePassword();
user.MobileServiceAuthenticationToken = credential.Password;
// Set the user from the stored credentials.
App.MobileService.CurrentUser = user;
try
{
// Try to return an item now to determine if the cached credential has expired.
await App.MobileService.GetTable<Event>().Take(1).ToListAsync();
}
catch (MobileServiceInvalidOperationException ex)
{
if (ex.Response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
// Remove the credential with the expired token.
vault.Remove(credential);
credential = null;
}
}
}
else
{
try
{
// Login with the identity provider.
user = await App.MobileService.LoginAsync(provider);
// Create and store the user credentials.
credential = new PasswordCredential(provider,
user.UserId, user.MobileServiceAuthenticationToken);
vault.Add(credential);
}
catch (MobileServiceInvalidOperationException ex)
{
Debug.WriteLine(ex.StackTrace);
}
}
}
示例9: SaveCredentials
public void SaveCredentials(string resource, string userName, string password) {
var vault = new PasswordVault();
RemoveAllCredentialsByResource(resource, vault);
// Add the new credential
var passwordCredential = new PasswordCredential(resource, userName, password);
vault.Add(passwordCredential);
}
示例10: SaveLogin
public void SaveLogin(string username, string password)
{
PasswordCredential pc;
var passwordVault = new Windows.Security.Credentials.PasswordVault();
var list = passwordVault.RetrieveAll().Where(i => String.Compare(i.Resource, RESOURCE) == 0);
if((pc = list.FirstOrDefault(i => String.Compare(i.UserName, username) == 0)) != null)
{
passwordVault.Remove(pc);
passwordVault.Add(new Windows.Security.Credentials.PasswordCredential(RESOURCE, username, password));
}
else if(list.Count() > 0)
{
foreach(var p in list)
{
passwordVault.Remove(p);
}
}
passwordVault.Add(new Windows.Security.Credentials.PasswordCredential(RESOURCE, username, password));
}
示例11: AuthenticateAsync
public async Task<User> AuthenticateAsync(string userName, string password, bool useSecureLogin = false)
{
User user = null;
if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
{
var client = this.GetHttpClient(useSecureLogin);
try
{
TokenResponse = await client.RequestResourceOwnerPasswordAsync(userName, password);
if (TokenResponse != null)
{
if (TokenResponse.IsError)
{
throw new UnauthorizedAccessException(this.m_strUnauthorizedAccessExceptionMessage);
}
Windows.Storage.ApplicationData.Current.RoamingSettings.Values["username"] = userName;
Windows.Storage.ApplicationData.Current.RoamingSettings.Values["usesecurelogin"] = useSecureLogin;
Windows.Security.Credentials.PasswordVault vault = new Windows.Security.Credentials.PasswordVault();
PasswordCredential passwordCredential = new PasswordCredential(PasswordVaultResourceName, userName, password);
vault.Add(passwordCredential);
if (passwordCredential != null)
{
user = new User
{
UserName = userName,
UseSecureLogin = useSecureLogin
};
m_settingsService.User = user;
}
}
}
catch (UnauthorizedAccessException)
{
throw new UnauthorizedAccessException(this.m_strUnauthorizedAccessExceptionMessage);
}
catch (Exception exception)
{
NullReferenceException nullReferenceException = exception as NullReferenceException;
if (nullReferenceException != null)
{
//there could be a nullreference exception at account change when the login is encrypted.
throw new UnauthorizedAccessException(this.m_strEncryptedLoginException);
}
throw exception;
}
}
else
{
throw new UnauthorizedAccessException(this.m_strUnauthorizedAccessExceptionMessage);
}
return user;
}
示例12: StoreToken
public static void StoreToken(string identifier, TokenResponse response)
{
var json = new JsonObject();
json["access_token"] = JsonValue.CreateStringValue(response.AccessToken);
json["expires_in"] = JsonValue.CreateNumberValue(response.ExpiresIn);
json["token_type"] = JsonValue.CreateStringValue(response.TokenType);
var vault = new PasswordVault();
vault.Add(new PasswordCredential(identifier, "token", json.Stringify()));
}
示例13: AuthenticateAsync
private async System.Threading.Tasks.Task AuthenticateAsync(String provider) {
string message;
// Use the PasswordVault to securely store and access credentials.
PasswordVault vault = new PasswordVault();
PasswordCredential credential = null;
while (credential == null) {
try {
// Try to get an existing credential from the vault.
credential = vault.FindAllByResource(provider).FirstOrDefault();
} catch (Exception) {
// When there is no matching resource an error occurs, which we ignore.
}
if (credential != null) {
// Create a user from the stored credentials.
_user = new MobileServiceUser(credential.UserName);
credential.RetrievePassword();
_user.MobileServiceAuthenticationToken = credential.Password;
// Set the user from the stored credentials.
App.MobileService.CurrentUser = _user;
try {
// Try to return an item now to determine if the cached credential has expired.
await App.MobileService.GetTable<TrainingItem>().Take(1).ToListAsync();
} catch (MobileServiceInvalidOperationException ex) {
if (ex.Response.StatusCode == System.Net.HttpStatusCode.Unauthorized) {
// Remove the credential with the expired token.
vault.Remove(credential);
credential = null;
continue;
}
}
} else {
try {
// Login with the identity provider.
_user = await App.MobileService.LoginAsync(provider);
// Create and store the user credentials.
credential = new PasswordCredential(provider, _user.UserId, _user.MobileServiceAuthenticationToken);
vault.Add(credential);
} catch (MobileServiceInvalidOperationException ex) {
message = "You must log in. Login Required";
}
}
message = string.Format("You are now logged in - {0}", _user.UserId);
var dialog = new MessageDialog(message);
dialog.Commands.Add(new UICommand("OK"));
await dialog.ShowAsync();
}
}
示例14: UpdateCredentialsInVault
public void UpdateCredentialsInVault(PasswordCredential passwordCredential)
{
var vault = new PasswordVault();
var credentials = GetCredentialsFromVault(vault);
if (credentials != null)
{
vault.Remove(credentials);
}
vault.Add(passwordCredential);
}
示例15: SetPassword
private static void SetPassword()
{
PasswordVault passwordVault = new PasswordVault();
var credentials = new PasswordCredential()
{
UserName = UserNameText,
Resource = ResourceText,
Password = PasswordGenerator.Next(PasswordLength)
};
passwordVault.Add(credentials);
}