2012-03-18 8 views
1

既存のメンバーシップをOpenIDに接続する方法を説明した記事がありますが、ユーザーがいくつかのOpenIDプロバイダーを使用して初めて自分のアカウントを作成したときに、彼の認証リンクがユーザー名として表示され、 comment.Howは、私は現在のユーザ名として表示するかを決定しています:メンバーシップのユーザー名とDotNetOpenAuth

string username = Membership.GetUser(UserID).UserName; 

      return string.IsNullOrEmpty(Membership.GetUser(UserID).Comment) ? username : Membership.GetUser(username).Comment; 

これは本当に問題ではありませんが、今私は、ユーザーのプロフィールページに何とかリンクする必要があり、私はそれを行う方法がわからないです、ここに私のために働くことができるものの例があります:

www.example.com/users/Guid/DisplayName 

表示名は、ユーザーがOpenIDプロバイダを使用してアカウントを作成した場合、私のページまたはコメントを介して登録した場合はユーザー名です。

私のようなものでした場合:

www.example.com/users/DisplayName 

を、私は誰かが会員と他のユーザーによってユーザー名「foo」をregeister可能性があるので、それは間違っているユーザーを表示しませんわからないのでOpenIDをして、そのユーザー名を使用しています彼は彼のコメント欄で "Foo"を得るだろう

私の質問を終わらせるために、私が他の多くのウェブサイトで同様のものを見ているようにユーザーGUIDをルーテッドURLに入れるのは悪いですか、GUIDから整数を派生させる方法はありますか行ったり来たり?

答えて

3

GUIDは確かにURLに入れることができます(恐らく、中括弧なし)。これとは別に、128ビットの数値として、GUIDよりも短いbase64文字列で表現することもできます。いずれのユーザーもユーザーにとって使い勝手が悪いですが、さまざまな種類のユーザーアカウント間の衝突についてのあなたの心配は正当だと思われます。

ここでは、GUIDをbase64のWebセーフティストリングに変換する方法を示します。コードスニペットはDotNetOpenAuthユーティリティの礼儀です)。

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(); 
    } 

そしてもちろんのGUIDにURLをbase64文字列から戻って変換します。

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()); 
    } 
+0

私が探していたまさに、ありがとうございました! – formatc

関連する問題