Created
August 23, 2017 06:49
-
-
Save ebicoglu/3effb448a0eb9cbec64b56eaae9dd2a4 to your computer and use it in GitHub Desktop.
Logining to AspNet Zero Website and get Access Token for api request via RestSharp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AjaxResponse<TResult> : AjaxResponseBase | |
{ | |
/// <summary> | |
/// The actual result object of AJAX request. | |
/// It is set if <see cref="AjaxResponseBase.Success"/> is true. | |
/// </summary> | |
public TResult Result { get; set; } | |
/// <summary> | |
/// Creates an <see cref="AjaxResponse"/> object with <see cref="Result"/> specified. | |
/// <see cref="AjaxResponseBase.Success"/> is set as true. | |
/// </summary> | |
/// <param name="result">The actual result object of AJAX request</param> | |
public AjaxResponse(TResult result) | |
{ | |
Result = result; | |
Success = true; | |
} | |
/// <summary> | |
/// Creates an <see cref="AjaxResponse"/> object. | |
/// <see cref="AjaxResponseBase.Success"/> is set as true. | |
/// </summary> | |
public AjaxResponse() | |
{ | |
Success = true; | |
} | |
/// <summary> | |
/// Creates an <see cref="AjaxResponse"/> object with <see cref="AjaxResponseBase.Success"/> specified. | |
/// </summary> | |
/// <param name="success">Indicates success status of the result</param> | |
public AjaxResponse(bool success) | |
{ | |
Success = success; | |
} | |
/// <summary> | |
/// Creates an <see cref="AjaxResponse"/> object with <see cref="AjaxResponseBase.Error"/> specified. | |
/// <see cref="AjaxResponseBase.Success"/> is set as false. | |
/// </summary> | |
/// <param name="error">Error details</param> | |
/// <param name="unAuthorizedRequest">Used to indicate that the current user has no privilege to perform this request</param> | |
public AjaxResponse(ErrorInfo error, bool unAuthorizedRequest = false) | |
{ | |
Error = error; | |
UnAuthorizedRequest = unAuthorizedRequest; | |
Success = false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class AjaxResponseBase | |
{ | |
/// <summary> | |
/// This property can be used to redirect user to a specified URL. | |
/// </summary> | |
public string TargetUrl { get; set; } | |
/// <summary> | |
/// Indicates success status of the result. | |
/// Set <see cref="Error"/> if this value is false. | |
/// </summary> | |
public bool Success { get; set; } | |
/// <summary> | |
/// Error details (Must and only set if <see cref="Success"/> is false). | |
/// </summary> | |
public ErrorInfo Error { get; set; } | |
/// <summary> | |
/// This property can be used to indicate that the current user has no privilege to perform this request. | |
/// </summary> | |
public bool UnAuthorizedRequest { get; set; } | |
/// <summary> | |
/// A special signature for AJAX responses. It's used in the client to detect if this is a response wrapped by ABP. | |
/// </summary> | |
public bool __abp { get; } = true; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AuthenticateModel | |
{ | |
public string UserNameOrEmailAddress { get; set; } | |
public string Password { get; set; } | |
public string TwoFactorVerificationCode { get; set; } | |
public bool RememberClient { get; set; } | |
public string TwoFactorRememberClientToken { get; set; } | |
public bool? SingleSignIn { get; set; } | |
public string ReturnUrl { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AuthenticateResultModel | |
{ | |
public string AccessToken { get; set; } | |
public string EncryptedAccessToken { get; set; } | |
public int ExpireInSeconds { get; set; } | |
public bool ShouldResetPassword { get; set; } | |
public string PasswordResetCode { get; set; } | |
public long UserId { get; set; } | |
public bool RequiresTwoFactorVerification { get; set; } | |
public IList<string> TwoFactorAuthProviders { get; set; } | |
public string TwoFactorRememberClientToken { get; set; } | |
public string ReturnUrl { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// This class holds access token for requests | |
/// </summary> | |
public static class Context | |
{ | |
public static AjaxResponse<AuthenticateResultModel> LoginResult = null; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ErrorInfo | |
{ | |
/// <summary> | |
/// Error code. | |
/// </summary> | |
public int Code { get; set; } | |
/// <summary> | |
/// Error message. | |
/// </summary> | |
public string Message { get; set; } | |
/// <summary> | |
/// Error details. | |
/// </summary> | |
public string Details { get; set; } | |
/// <summary> | |
/// Validation errors if exists. | |
/// </summary> | |
public ValidationErrorInfo[] ValidationErrors { get; set; } | |
/// <summary> | |
/// Creates a new instance of <see cref="ErrorInfo"/>. | |
/// </summary> | |
public ErrorInfo() | |
{ | |
} | |
/// <summary> | |
/// Creates a new instance of <see cref="ErrorInfo"/>. | |
/// </summary> | |
/// <param name="message">Error message</param> | |
public ErrorInfo(string message) | |
{ | |
Message = message; | |
} | |
/// <summary> | |
/// Creates a new instance of <see cref="ErrorInfo"/>. | |
/// </summary> | |
/// <param name="code">Error code</param> | |
public ErrorInfo(int code) | |
{ | |
Code = code; | |
} | |
/// <summary> | |
/// Creates a new instance of <see cref="ErrorInfo"/>. | |
/// </summary> | |
/// <param name="code">Error code</param> | |
/// <param name="message">Error message</param> | |
public ErrorInfo(int code, string message) | |
: this(message) | |
{ | |
Code = code; | |
} | |
/// <summary> | |
/// Creates a new instance of <see cref="ErrorInfo"/>. | |
/// </summary> | |
/// <param name="message">Error message</param> | |
/// <param name="details">Error details</param> | |
public ErrorInfo(string message, string details) | |
: this(message) | |
{ | |
Details = details; | |
} | |
/// <summary> | |
/// Creates a new instance of <see cref="ErrorInfo"/>. | |
/// </summary> | |
/// <param name="code">Error code</param> | |
/// <param name="message">Error message</param> | |
/// <param name="details">Error details</param> | |
public ErrorInfo(int code, string message, string details) | |
: this(message, details) | |
{ | |
Code = code; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoginHelper | |
{ | |
private const string BaseUrl = "http://MyAspnetZeroApplication.com/"; | |
public async Task<bool> Authenticate(string username, string password) | |
{ | |
try | |
{ | |
var request = new RestRequest("api/TokenAuth/Authenticate", Method.POST) | |
{ | |
Timeout = 5000 | |
}; | |
request.AddHeader("Accept", "application/json; charset=UTF-8"); | |
request.AddHeader("Accept-Language", "en-US"); | |
request.AddHeader("Content-Type", "application/json; charset=UTF-8"); | |
request.AddHeader("User-Agent", "Abp.Mobile.Client"); | |
request.AddHeader("X-Requested-With", "XMLHttpRequest"); | |
request.RequestFormat = DataFormat.Json; | |
request.AddBody(new AuthenticateModel | |
{ | |
UserNameOrEmailAddress = username, | |
Password = password | |
}); | |
var client = new RestClient(BaseUrl); | |
var response = await client.ExecuteGetTaskAsync<AjaxResponse<AuthenticateResultModel>>(request); | |
if (response.StatusCode != HttpStatusCode.OK) | |
{ | |
return false; | |
} | |
Context.LoginResult = response.Data; | |
return response.Data.Success && !string.IsNullOrEmpty(response.Data.Result.AccessToken); | |
} | |
catch (Exception ex) | |
{ | |
//ERROR HANDLING | |
} | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Used to store information about a validation error. | |
/// </summary> | |
public class ValidationErrorInfo | |
{ | |
/// <summary> | |
/// Validation error message. | |
/// </summary> | |
public string Message { get; set; } | |
/// <summary> | |
/// Relate invalid members (fields/properties). | |
/// </summary> | |
public string[] Members { get; set; } | |
/// <summary> | |
/// Creates a new instance of <see cref="ValidationErrorInfo"/>. | |
/// </summary> | |
public ValidationErrorInfo() | |
{ | |
} | |
/// <summary> | |
/// Creates a new instance of <see cref="ValidationErrorInfo"/>. | |
/// </summary> | |
/// <param name="message">Validation error message</param> | |
public ValidationErrorInfo(string message) | |
{ | |
Message = message; | |
} | |
/// <summary> | |
/// Creates a new instance of <see cref="ValidationErrorInfo"/>. | |
/// </summary> | |
/// <param name="message">Validation error message</param> | |
/// <param name="members">Related invalid members</param> | |
public ValidationErrorInfo(string message, string[] members) | |
: this(message) | |
{ | |
Members = members; | |
} | |
/// <summary> | |
/// Creates a new instance of <see cref="ValidationErrorInfo"/>. | |
/// </summary> | |
/// <param name="message">Validation error message</param> | |
/// <param name="member">Related invalid member</param> | |
public ValidationErrorInfo(string message, string member) | |
: this(message, new[] { member }) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment