2016-10-07 11 views
0

私はMVC C#で初心者です。今私はロールプロバイダとのカスタム認証を構築しようとしていますが、ユーザロールをチェックするときに「値はnullではない」というエラーが発生します。ここ は私RoleProvider:ロールプロバイダで値をnullにすることはできません

public class MyRoleProvider:RoleProvider 
{ 
    private int _cacheTimeoutInMinute = 20; 
    LoginGateway gateway=new LoginGateway(); 


public override string[] GetRolesForUser(string username) 
    { 
     if (!HttpContext.Current.User.Identity.IsAuthenticated) 
     { 
      return null; 
     } 

     //check cache 
     var cacheKey = string.Format("{0}_role", username); 
     if (HttpRuntime.Cache[cacheKey] != null) 
     { 
      return (string[])HttpRuntime.Cache[cacheKey]; 
     } 
     string[] roles 

      = gateway.GetUserRole(username).ToArray(); 
      if (roles.Any()) 
      { 
       HttpRuntime.Cache.Insert(cacheKey, roles, null, DateTime.Now.AddMinutes(_cacheTimeoutInMinute), Cache.NoSlidingExpiration); 

      } 

     return roles; 
    } 
public override bool IsUserInRole(string username, string roleName) 
    { 
     var userRoles = GetRolesForUser(username); 
      return userRoles.Contains(roleName); 

    } 

そのは常にreurn userRoles.Contains(roleName);(値はNULLにすることはできません(ユーザー名))の行にエラーを得ています。私はデバッグポインタを使用して、それはゲートウェイが呼び出されたことがないことを示しています。私は、string[] roles = gateway.GetUserRole(username).ToArray();ので、常にnullのままです。 私は、ゲートウェイ上の私のGetUserRole方法が正しいかではないことを確認していないが:ここに私のゲートウェイです:

public string[] GetUserRole(string userName) 
    { 

     string role = null; 
     SqlConnection connection = new SqlConnection(connectionString); 
     string query = "select r.RoleType from RoleTable r join UserRoleTable ur on ur.RoleId=r.Id join UserTable u on u.UserId=ur.UserId where u.UserName='"+userName+"'"; 
     SqlCommand command = new SqlCommand(query, connection); 
     connection.Open(); 
     SqlDataReader reader = command.ExecuteReader(); 
     while (reader.Read()) 
     { 
      role = reader["RoleType"].ToString(); 

     } 
     string[] roles = {role}; 
     reader.Close(); 
     connection.Close(); 
     return roles; 
    } 

は私のコードに問題はありませんか?どうすればこのエラーを解決できますか?

+0

をあなたのハードコードならば、それは 'GetUserRole'メソッドで戻り値を動作します - ここで

は、あなたが使用したい場合はキャッシュサービスですか?たとえば、 'public string [] GetUserRole(string userName){return new string [] {" Somerole "}; } ' – Win

+0

httpContextとキャッシュのすべてのコードを削除またはコメントアウトすると動作します...キャッシュ部分のコードに問題がありますか? @勝つ –

答えて

0

httpContextとすべてのコードを削除したり、コメントアウトすると動作します キャッシュ...キャッシュ部分のコードに問題がありますか? @Win

エンタープライズアプリケーションを開発していない限り、スピードが問題であり、すべてのクエリがカウントされない限り、私はデータベースを叩くことは心配しません。 オリジナルのASP.Netメンバーシップ・プロバイダは、キャッシュをまったく使用しません。

さらに、ASP.Netメンバシッププロバイダは、非常に古い技術 - より12年古いです。エンタープライズアプリケーション用に実装する場合は、クレームベース認証の使用を検討することをお勧めします。

using System; 
using System.Collections.Generic; 
using System.Runtime.Caching; 

public class CacheService 
{ 
    protected ObjectCache Cache => MemoryCache.Default; 

    public T Get<T>(string key) 
    { 
     return (T) Cache[key]; 
    } 

    public void Set(string key, object data, int cacheTime) 
    { 
     if (data == null) 
      return; 

     var policy = new CacheItemPolicy(); 
     policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime); 
     Cache.Add(new CacheItem(key, data), policy); 
    } 

    public void Remove(string key) 
    { 
     Cache.Remove(key); 
    } 
} 
関連する問題