I am using ASP.NET MVC 4, the .NET Braintree Payments API, and Braintree.js.
Here is a simple wrapper I have built for Braintree:
public class PaymentBL
{
private static BraintreeGateway _braintreeGateway = new BraintreeGateway
{
Environment = Braintree.Environment.SANDBOX,
MerchantId = "xxxxxxx",
PublicKey = "xxxxxxxxxxxx",
PrivateKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
};
public Result<Transaction> ChargeCardOnce(decimal amount, string cardholderName, string cardNumber, string expiration,
string cvv)
{
TransactionCreditCardRequest creditCardRequest = new TransactionCreditCardRequest();
creditCardRequest.CardholderName = cardholderName;
creditCardRequest.Number = cardNumber;
creditCardRequest.ExpirationDate = expiration;
creditCardRequest.CVV = cvv;
TransactionOptionsRequest optionsRequest = new TransactionOptionsRequest();
optionsRequest.SubmitForSettlement = true;
TransactionRequest transactionRequest = new TransactionRequest();
transactionRequest.Amount = amount;
transactionRequest.CreditCard = creditCardRequest;
transactionRequest.Options = optionsRequest;
return _braintreeGateway.Transaction.Sale(transactionRequest);
}
/// <summary>
/// Stores a credit card in the Braintree vault. In some cases, will put a $1 temporary charge
/// on the credit card that will come off a few days later.
///
/// From BrainTree: Regardless of card type, any instance where a $1 authorization returns a successful result,
/// we immediately follow-up with an automatic void request to ensure that the transaction will fall off
/// of the cardholder's statement as soon as possible.
/// </summary>
public Result<CreditCard> StoreCustomer(int customerId, string cardholderName, string cardNumber, string expiration, string cvv)
{
//CreditCardAddressRequest addressRequest = new CreditCardAddressRequest();
//addressRequest.PostalCode = postalCode;
CreditCardOptionsRequest optionsRequest = new CreditCardOptionsRequest();
optionsRequest.VerifyCard = true;
optionsRequest.VerificationMerchantAccountId = _braintreeGateway.MerchantId;
CreditCardRequest creditCard = new CreditCardRequest();
creditCard.CustomerId = customerId.ToString();
creditCard.Token = customerId.ToString(); // Use same token to ensure overwrite
creditCard.CardholderName = cardholderName;
creditCard.Number = cardNumber;
creditCard.ExpirationDate = expiration;
creditCard.CVV = cvv;
creditCard.Options = optionsRequest;
return _braintreeGateway.CreditCard.Create(creditCard);
}
/// <summary>
/// Search BrainTree vault to retrieve credit card
/// </summary>
/// <param name="customerId"></param>
public CreditCard GetCreditCardOnFile(int customerId)
{
Customer customer = null;
try
{
customer = _braintreeGateway.Customer.Find(customerId.ToString());
}
catch (Braintree.Exceptions.NotFoundException)
{
return null;
}
if (customer.CreditCards == null || customer.CreditCards.Length == 0)
{
return null;
}
if (customer.CreditCards.Length >= 2)
{
throw new Exception(string.Format("Customer {0} has {1} credit cards",
customerId, customer.CreditCards.Length));
}
return customer.CreditCards[0];
}
}
When I call this method, it works:
Result<Transaction> result = paymentBL.ChargeCardOnce(
2.34m
, formCollection["name"]
, formCollection["number"]
, formCollection["exp"]
, formCollection["cvv"]
);
Subsequently, I can view the completed test transactions in the Braintree dashboard. Therefore, I know that the encrypted form values from Braintree.js are arriving at my controller action correctly, and that my keys and merchant account IDs are all set up correctly.
When I replace the above call to ChargeCardOnce with the below call to StoreCustomer, I receive an Braintree.Exceptions.AuthorizationException at the line return _braintreeGateway.CreditCard.Create(creditCard);
Result<CreditCard> result = paymentBL.StoreCustomer(
systemHost.Customer.CustomerId
, formCollection["name"]
, formCollection["number"]
, formCollection["exp"]
, formCollection["cvv"]
);
From Braintree support: "You are able to create a customer as well as a credit card in the sandbox, as it is built to exactly mirror what the production environment would look like."
Has anyone experience this also? I'm referring Braintree support to this question, but if anyone on SO has seen this and knows a solution or workaround, I would be much relieved.
I work at Braintree. It looks like we already got back to you with the answer to your question, but I'll answer here as well for anyone who has the same issue in the future.
In this case, the problem is:
optionsRequest.VerificationMerchantAccountId = _braintreeGateway.MerchantId;
Your merchant ID identifies your payment gateway account, while your merchant account ID identifies the bank account you want to use for the verification. An article in our support center explains the difference:
MerchantAccountId
With Braintree, you can have multiple merchant
accounts all processing via the same gateway account. This
means that you can have multiple locations, multiple businesses,
multiple currencies, etc. all setup and processing under a single
account. This makes it easy to keep track of all of your processing
via unified reporting and access and can even save you money.
You can find the IDs for all merchant accounts in your gateway
account by following these steps:
login to your account
hover over your account name
and click "Processing"
scroll to the bottom of the page to the
section labeled "Merchant Accounts".
An AuthorizationException is HTTP Status Code 403 Forbidden. It means that the server is declining your request, because you don't have permission to access a resource (even though you may be authenticated).
Since there is no merchant account available to your user with the ID you specify (since it's not a merchant account ID at all), you get the AuthorizationException.
If, as is often the case, your merchant has only one merchant account, or you want to use the default account, it's not necessary to specify a VerificationMerchantAccountId.
Related
I have a C# application in which I imported API methods using wsdl, as described in the Softlayer guidelines.
I'm editing virtual guests by passing a Container_Product_Order_Virtual_Guest_Upgrade structure to the Product_Order service.
Everything works great except for when adding item price IDs for guest_disks, scenario in which after about 6-7 seconds the following exception is thrown:
"The request was aborted: The connection was closed unexpectedly."
This happens with both verifyOrder and placeOrder methods.
I checked Virtual_Guest::getUpgradeItemPrices in order to make sure that the guest disk values are valid(even though passing invalid itempriceIds for the VM results in an specific error response, not in a generic exception such as the one described above).
I can't find any details in the documentation that could give me hints as why I can upgrade anything except guest_disks.
EDIT:
Stripped code as requested:
SoftLayer_Virtual_Guest[] _VMtoEditList = new SoftLayer_Virtual_Guest[1] { -- Vm instance details are retrieved from SL according to the passed VM ID; };
List<SoftLayer_Product_Item_Price> _itemPriceList = new List<SoftLayer_Product_Item_Price>();
foreach (-- collection of properties to be upgraded )
{
SoftLayer_Product_Item_Category _category = new SoftLayer_Product_Item_Category();
_category.categoryCode = -- retrieved from the collection on which I iterate (eg "guest_disk0", "ram", etc.);
SoftLayer_Product_Item_Price _itemPrice = new SoftLayer_Product_Item_Price();
_itemPrice.id = -- the item priceID for the current item;
_itemPrice.idSpecified = true;
_itemPrice.categories = new SoftLayer_Product_Item_Category[1] { _category };
_itemPriceList.Add(_itemPrice);
}
SoftLayer_Product_Item_Price[] _itemPricesArray = _itemPriceList.ToArray();
SoftLayer_Container_Product_Order_Property _property1 = new SoftLayer_Container_Product_Order_Property();
_property1.name = "NOTE_GENERAL";
_property1.value = -- order's description;
SoftLayer_Container_Product_Order_Property _property2 = new SoftLayer_Container_Product_Order_Property();
_property2.name = "MAINTENANCE_WINDOW";
_property2.value = "now";
// Build SoftLayer_Container_Product_Order_Property
SoftLayer_Container_Product_Order_Property[] properties = new SoftLayer_Container_Product_Order_Property[2] { _property1, _property2 };
-- create container
SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade _upgradeContainer = new SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade();
_upgradeContainer.virtualGuests = _VMtoEditList;
_upgradeContainer.prices = _itemPricesArray;
_upgradeContainer.properties = properties;
_upgradeContainer.packageId = 46;
_upgradeContainer.packageIdSpecified = true;
SoftLayer_Product_OrderService service = new SoftLayer_Product_OrderService();
-- authentication structure is created here
SoftLayer_Container_Product_Order _verifiedOrder = service.verifyOrder(_upgradeContainer);
service.placeOrder(_verifiedOrder, false);
Here a have an example to upgrade which works see below. I see that in your code you are adding the packageId which is not required, removed and try again.
Also when you are creating the web references try using the last version of the api in the WSDL url (v3.1)
e.g. https://api.softlayer.com/soap/v3.1/SoftLayer_Hardware_Server?wsdl
//-----------------------------------------------------------------------
// <copyright file="PlaceOrderUpgrade.cs" company="Softlayer">
// SoftLayer Technologies, Inc.
// </copyright>
// <license>
// http://sldn.softlayer.com/article/License
// </license>
//-----------------------------------------------------------------------
namespace VirtualGuests
{
using System;
using System.Collections.Generic;
class PlaceOrderUpgrade
{
/// <summary>
/// Order an upgrade for Virtual Guest
/// This script orders an upgrade for Virtual Guest, in this case we will upgrade the ram for a Virtual Guest,
/// It uses SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade container and SoftLayer_Product_Order::placeOrder
/// method for it.
/// For more information, review the following links:
/// </summary>
/// <manualPages>
/// http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/placeOrder
/// http://sldn.softlayer.com/reference/datatypes/SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade/
/// http://sldn.softlayer.com/reference/services/SoftLayer_Product_Item_Price/
/// </manualPages>
static void Main(String [] args)
{
// You SoftLayer username
string username = "set me";
// Your SoftLayer API key.
string apiKey = "set me";
// Define the virtual guest id to place an upgrade
int virtualId = 13115425;
// Creating a connection to the SoftLayer_Product_Order API service and
// bind our API username and key to it.
authenticate authenticate = new authenticate();
authenticate.username = username;
authenticate.apiKey = apiKey;
SoftLayer_Product_OrderService orderService = new SoftLayer_Product_OrderService();
orderService.authenticateValue = authenticate;
// Build a SoftLayer_Product_Item_Price objects with the ids from prices that you want to order.
// You can retrieve them with SoftLayer_Product_Package::getItemPrices method
int[] prices = {
1645
};
List<SoftLayer_Product_Item_Price> pricesList = new List<SoftLayer_Product_Item_Price>();
foreach (var price in prices)
{
SoftLayer_Product_Item_Price newPrice = new SoftLayer_Product_Item_Price();
newPrice.id = price;
newPrice.idSpecified = true;
pricesList.Add(newPrice);
}
// Build SoftLayer_Container_Product_Order_Property object for the upgrade
SoftLayer_Container_Product_Order_Property property = new SoftLayer_Container_Product_Order_Property();
property.name = "MAINTENANCE_WINDOW";
property.value = "NOW";
List<SoftLayer_Container_Product_Order_Property> propertyList = new List<SoftLayer_Container_Product_Order_Property>();
propertyList.Add(property);
// Build SoftLayer_Virtual_Guest object with the id from vsi that you wish to place an upgrade
SoftLayer_Virtual_Guest virtualGuest = new SoftLayer_Virtual_Guest();
virtualGuest.id = virtualId;
virtualGuest.idSpecified = true;
List<SoftLayer_Virtual_Guest> virtualGuests = new List<SoftLayer_Virtual_Guest>();
virtualGuests.Add(virtualGuest);
// Build SoftLayer_Container_Product_Order object containing the information for the upgrade
//SoftLayer_Container_Product_Order orderTemplate = new SoftLayer_Container_Product_Order();
SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade orderTemplate = new SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade();
orderTemplate.containerIdentifier = "SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade";
orderTemplate.prices = pricesList.ToArray();
orderTemplate.properties = propertyList.ToArray();
orderTemplate.virtualGuests = virtualGuests.ToArray();
try
{
// We will check the template for errors, we will use the verifyOrder() method for this.
// Replace it with placeOrder() method when you are ready to order.
SoftLayer_Container_Product_Order verifiedOrder = orderService.verifyOrder(orderTemplate);
Console.WriteLine("Order Verified!");
}
catch (Exception e)
{
Console.WriteLine("Unable to place an upgrade for Virtual Guest: " + e.Message);
}
}
}
}
Let me know if this helps
Regards
currently I´m writing on a outlook plugin for syncing goolge contacts with outlook but I have to cover some special case:
When a contact gets deleted on google side, my application detects the missing contact and creates a new contact based on the contact info from the outlook one.
Is there a way to get an event or history from google that tells me a user deleted this contact(s)?
Edit 1:
Here is my code how I´m accessing the contacts (what is working FINE):
public GoogleAccessor()
{
var parameters = new OAuth2Parameters()
{
ClientId = CLIENTID,
ClientSecret = CLIENTSECRET,
RedirectUri = REDIRECTURI,
Scope = SCOPES
};
string url = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
//An own webbrowser for processing the access tokens
IAuthorizationCodeProvider authcodeProvider = new Presentation.BrowserAuthorizationCodeProvider(new Presentation.BrowserAuthentificatorVM());
parameters.AccessCode = authcodeProvider.GetAuthorizationCode(url);
if(parameters.AccessCode == null)
throw new GoogleOAuthException("AccesCode returned 'null' and failed!");
OAuthUtil.GetAccessToken(parameters);
this._contactsRequest = new ContactsRequest(new RequestSettings(APPLICATIONNAME, parameters) {AutoPaging = true});
}
public IList<IContact> GetAllMappedContacts()
{
Feed<Google.Contacts.Contact> f = _contactsRequest.GetContacts();
this._feedUri = new Uri(f.AtomFeed.Feed);
var photoList = new List<PhotoObject>();
foreach (var entry in f.Entries)
{
var photoObject = GetContactPhoto(entry);
if(photoObject != null)
photoList.Add(photoObject);
}
_googleMapper = new GoogleMapper(f.Entries);
return _googleMapper.MapToLocalContacts();;
}
The thing about syncing in general is that syncing is normally meant to work in one direction.
Source Data -> Data Flow -> Received Data.
In this instance, Outlook is your source data and Google is your received data. All information needs to come from your source. Since this is an Outlook add-in you are creating my suggestion would be to add a button to your add-in ribbon. You can call the button whatever ever you like (maybe "dontSyncButton"), but it's purpose is going to be Categorization of your contact.
Make it so that that when a contact is selected and then the button is clicked, the contact is given a special categorization (perhaps "Dont Sync").
Now give some logic to your code that executes the sync, and have that logic decide whether to sync the contact. Also, give some logic to tell the program to delete the contact out of Google for you if the contacts contains the special category. Semi-Pseudo Code below:
if(contact.Categories.ToString() == "Dont Sync")
{
//Don't Sync Contact
If(googleContact.Exists())
{
//Delete contact from Google if it exist
googleContact.Delete();
}
}
else
{
//Sync Contact
}
It would be nice if Outlook had many modifiable properties that weren't visible to users, but since it does not this is really one of the best options I can think of. I do this to sync contacts from a shared Outlook folder to personal ones and it has worked well so far.
Hope this helps!
The goal
Create a new market and add its manager successfully.
The problem
Entity Validation Failed - errors follow:
MyApp.Models.Data.Users failed validation
Email : The Email field is required.
PHash : The PHash field is required.
PSalt : The PSalt field is required.
The scenario
I'm creating a market on my application and I can set to it a manager. In other words, I'm attaching to a market a user that already exists. I'm trying to do this by the following code:
[HttpPost]
public ActionResult Add(Market market)
{
[...]
Market marketBasics = new Market
{
Name = market.Name,
Slug = market.Slug,
Manager = market.ManagerId
};
[...]
User user = new User
{
Id = market.ManagerId
};
db.Markets.Add(marketBasics); // Here I insert all the information
// into "markets" table.
marketBasics.User.Add(user); // Here I (attempts to) insert into
// "users_in_markets"
[...]
}
As you can see, I'm not setting the user's Email, PHash or PSalt — I'm not registering a new user, but I'm associating an existing (user) to a (new) market.
So I ask: How can I resolve this?
Technical details
I'm using Entity Framework 5 + C# + MySQL.
The user is not added because you add the user to the market after the market has been added to the database. Therefore the user is not present in the market in the database.
marketBasics.User.Add(user); //First add user to market
db.Markets.Add(marketBasics); //Then add market to database
I hope that helped!
With the answer of Davud and a little research, I discover that I need to attach the user's Id to the User entity.
Follow the final code:
[HttpPost]
public ActionResult Add(Market market)
{
[...]
Market marketBasics = new Market
{
Name = market.Name,
Slug = market.Slug,
Manager = market.ManagerId
};
[...]
User user = new User
{
Id = market.ManagerId
};
db.User.Attach(user);
marketBasics.User.Add(user);
db.Markets.Add(marketBasics);
[...]
}
To learn more about Attach method, take a look here, on MSDN.
Success for you all!
In CRM when emails arrive and have the tracking token in them they automatically set the regarding field to be the incident (or whatever they relate to)
Unfortunately the Record wall isn't updated with this info so even if you are following the case nothing alerts you to the new activity.
I want to write a plugin on email or incident (or both) that updates the record wall and creates a task to follow up on that email with in 3 days.
I'm looking at the SDK and I can't see what the appropriate event in the pipe line would be to work out when an email is/has its regarding field set on arrival in the CRM.
The CRM email creation life-cycle is not well described in the documentation. [shakes fist]
Extra things that are bothering me
I can't seem to include a reference to get a strongly typed Email, Post or Case (driving me crazy)
Testing this is really hard (harder than it should be)
EDIT Here is my current code
namespace Assembly.Plugins
{
using System;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
/// <summary>
/// PostEmailDeliverIncoming Plugin.
/// </summary>
public class PostEmailDeliverIncoming : Plugin
{
/// <summary>
/// Initializes a new instance of the <see cref="PostEmailDeliverIncoming"/> class.
/// </summary>
public PostEmailDeliverIncoming()
: base(typeof(PostEmailDeliverIncoming))
{
RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "DeliverIncoming", "email", ExecutePostEmailDeliverIncoming));
// Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.
// You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.
}
protected void ExecutePostEmailDeliverIncoming(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService = localContext.TracingService;
// Obtain the execution context from the service provider.
IPluginExecutionContext context = localContext.PluginExecutionContext;
// Obtain the organization service reference.
var service = localContext.OrganizationService;
// The InputParameters collection contains all the data passed in the message request.
if (!context.InputParameters.Contains("Target") || !(context.InputParameters["Target"] is Entity))
return;
// Obtain the target entity from the input parmameters.
var target = (Entity)context.InputParameters["Target"];
// Verify that the target entity represents an account.
// If not, this plug-in was not registered correctly.
if (target.LogicalName != "email")
return;
if((string)target["direction"] != "Incoming")
return;
if (target["regardingobjectid"] == null)
return;
try
{
// if its not a case I don't care
var incident = service.Retrieve("incident", (Guid)target["regardingobjectid"], new ColumnSet(true));
if (incident == null)
return;
var post = new Entity("post");
post["regardingobjectid"] = target["regardingobjectid"];
post["source"]=new OptionSetValue(0);
post["text"] = String.Format("a new email has arrived.");
// Create the task in Microsoft Dynamics CRM.
tracingService.Trace("FollowupPlugin: Creating the post.");
service.Create(post);
// Create a task activity to follow up with the account customer in 7 days.
var followup = new Entity("task");
followup["subject"] = "Follow up incoming email.";
followup["description"] = "An email arrived that was assigned to a case please follow it up.";
followup["scheduledstart"] = DateTime.Now.AddDays(3);
followup["scheduledend"] = DateTime.Now.AddDays(3);
followup["category"] = context.PrimaryEntityName;
// Refer to the email in the task activity.
if (context.OutputParameters.Contains("id"))
{
var regardingobjectid = new Guid(context.OutputParameters["id"].ToString());
followup["regardingobjectid"] = new EntityReference("email", regardingobjectid);
}
// Create the task in Microsoft Dynamics CRM.
tracingService.Trace("FollowupPlugin: Creating the task activity.");
service.Create(followup);
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in the FollupupPlugin plug-in.", ex);
}
catch (Exception ex)
{
tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
throw;
}
}
}
}
I've just been fighting with this exact same issue and came across this post. I thought I'd post the solution for you (if you still need it) and anyone else who comes across the issue in the future.
Here's the solution I arrived at:
- Using the Plugin Registration Tool register a New Image on the appropriate step( Stage = "40", MessageName = "DeliverIncoming")
- Set the New Image to be a Post Image
- In your plugin fetch the Post Image's entity ID:
Guid emailID = context.PostEntityImages["PostImage"].Id;
Entity emailFromRetrieve = localContext.OrganizationService.Retrieve(
"email",
emailID,
new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
Email email = emailFromRetrieve.ToEntity<Email>();
if (email.RegardingObjectId == null)
{
return;
}
var regardingObject = email.RegardingObjectId;
Hope this helps!
I'm actually working on a very similar plugin at the moment. Mine creates a custom entity upon arrival of an email addressed to a certain email address. It also associates the incoming email with that new record via the Regarding field. I've added a Pre-Operation step on Create of Email and it works great, including incoming email from the router.
What I'm not sure of is when CRM fills in the Regarding field. You might look at Post-Operation and see if it is set there?
One interesting caveat regarding the Regarding field (haha!): Unlike single lookup fields, the Regarding object's name is actually stored in the ActivityPointer table, so when you update the Regarding field, be sure to set the Name on the EntityReference. If you don't, the Regarding lookup will still have a clickable icon but there won't be any text. I do it like this:
email.RegardingObjectId = [yourentity].ToEntityReference();
email.RegardingObjectId.Name = email.Subject;
Hope that helps!
I ended up doing this in a workflow on the email entity
Steps
Create new workflow, I called it 'incoming email workflow'
Scope is Organisation
Choose Email as the entity and check 'Record field changes'
Add a step that checks Regarding (Case):Case Contains Data
if true:
Add a step that creates a Post
Edit the properties in the Post
Text : This case has had {Direction(E-mail)} email activity from {From(E-mail)}
Source : Auto Post
Regarding : {Regarding(E-mail)}
Add a step that creates a Task
Edit the properties in the Task
Subject : Follow up {Subject(E-mail)}
Regarding : {Regarding(E-mail)}
Try to use the following code:
if ((bool)entity["directioncode"] == false)
Instead of your code:
if((string)target["direction"] != "Incoming")
I found an article describing how to connect existing membership with OpenID but when user uses some OpenID provider to login first time my app creates account from him, it puts his authenticate link as username, and display name as comment.How am I currently determining what to show as username:
string username = Membership.GetUser(UserID).UserName;
return string.IsNullOrEmpty(Membership.GetUser(UserID).Comment) ? username : Membership.GetUser(username).Comment;
This really isn't a problem,but now I have to link somehow to user profile page, and I am not sure how to do that, here is an example of what could work for me:
www.example.com/users/Guid/DisplayName
Display name is either username if he registered through my page or comment if user used OpenID provider do create account.
if I did something like:
www.example.com/users/DisplayName
I'm not sure it won't display wrong user since someone could regeister username "Foo" through membership and some other user is using that username with OpenID so he would get "Foo" in his comment field
So to finish my question, would it be bad to put user GUID in routed url as I saw similar stuff on many other websites,or is there way to derive integer from GUID back and forth?
A GUID can certainly be put into a URL (probably without curly braces around it). Alternatively as a 128-bit number, it can also be represented in a base64 string, which would be shorter than a GUID. Either one is pretty user-unfriendly, but your concern about collisions between different types of user accounts seems justified.
Here is how you could convert a GUID into a base64 web-safe string. Code snippets courtesy of DotNetOpenAuth utilities).
Guid userGuid; // value comes from your database
ConvertToBase64WebSafeString(userGuid.ToByteArray());
/// <summary>
/// Converts to data buffer to a base64-encoded string, using web safe characters and with the padding removed.
/// </summary>
/// <param name="data">The data buffer.</param>
/// <returns>A web-safe base64-encoded string without padding.</returns>
internal static string ConvertToBase64WebSafeString(byte[] data) {
var builder = new StringBuilder(Convert.ToBase64String(data));
// Swap out the URL-unsafe characters, and trim the padding characters.
builder.Replace('+', '-').Replace('/', '_');
while (builder[builder.Length - 1] == '=') { // should happen at most twice.
builder.Length -= 1;
}
return builder.ToString();
}
And of course convert back from the URL base64 string to a Guid:
string base64SegmentFromUrl; // from incoming web request to profile page
Guid userGuid = new Guid(FromBase64WebSafeString(base64SegmentFromUrl);
/// <summary>
/// Decodes a (web-safe) base64-string back to its binary buffer form.
/// </summary>
/// <param name="base64WebSafe">The base64-encoded string. May be web-safe encoded.</param>
/// <returns>A data buffer.</returns>
internal static byte[] FromBase64WebSafeString(string base64WebSafe) {
Requires.NotNullOrEmpty(base64WebSafe, "base64WebSafe");
Contract.Ensures(Contract.Result<byte[]>() != null);
// Restore the padding characters and original URL-unsafe characters.
int missingPaddingCharacters;
switch (base64WebSafe.Length % 4) {
case 3:
missingPaddingCharacters = 1;
break;
case 2:
missingPaddingCharacters = 2;
break;
case 0:
missingPaddingCharacters = 0;
break;
default:
throw ErrorUtilities.ThrowInternal("No more than two padding characters should be present for base64.");
}
var builder = new StringBuilder(base64WebSafe, base64WebSafe.Length + missingPaddingCharacters);
builder.Replace('-', '+').Replace('_', '/');
builder.Append('=', missingPaddingCharacters);
return Convert.FromBase64String(builder.ToString());
}