ios - 为什么身份验证不适用于我的 ASP.NET REST API?

标签 ios asp.net objective-c rest

我设置了一个 ASP.NET REST API,并尝试通过 iOS 进行连接。同样的错误继续出现,我不确定连接在哪里“断开”。在 NSLog 中,链接出现时带有两个斜杠,而不是像 NSString 中那样带有正斜杠。谁能告诉我为什么这没有连接?

完成 block 日志:

2015-02-08 23:19:00.795 b2bGatewayWebview[7266:548905] Loading... ["method","interface","parameters","UserAuthentication","http:\/\/website.com\/folder\/Handler1.ashx",{"userName":"DummyAcct","passsword":"DummyPwd"}]
2015-02-08 23:19:00.796 b2bGatewayWebview[7266:548905] Loading... {"method":"Getmembers","interface":"http:\/\/website.com\/folder\/Handler1.ashx","parameters":{"username":"DummyAcct"}}
2015-02-08 23:19:00.795 b2bGatewayWebview[7266:548861] View did load called.
2015-02-08 23:19:01.525 b2bGatewayWebview[7266:548937] done
2015-02-08 23:19:01.537 b2bGatewayWebview[7266:548937] RAW response = {
  "Successful": false,
  "ErrorMessage": "Internal server error"
}

RestAPI.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface RestAPI: UIViewController

@property (nonatomic, strong) NSURL *url;

@property (nonatomic, strong) NSString *interface;

-(void) CreateNewAccount:(NSString*)ausername password:(NSString*)apassword completionHandler:(void(^)(NSDictionary *dictionary, NSError *error))handler;

-(void) Getmembers:(NSString*)ausername completionHandler:(void(^)(NSDictionary *dictionary, NSError *error))handler;

-(void) UserAuthentication:(NSString*)auserName passsword:(NSString*)apasssword completionHandler:(void(^)(NSDictionary *dictionary, NSError *error))handler;

@end

RestAPI.m

#import "RestAPI.h"

@implementation RestAPI

@synthesize url;
@synthesize interface;

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *initialLink = @"http://website.com/Handler1.ashx";


    [self setInterface:initialLink];

    NSString *username = @"DummyAcct";
    NSString *password = @"DummyPswd";

    //asynchronously call login method
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [self UserAuthentication:username passsword:password completionHandler:^(NSDictionary *dictionary, NSError *error) {
        NSLog(@"Login method called.");
    }];


    [self Getmembers:username completionHandler:^(NSDictionary *dictionary, NSError *error) {
            nil;
    }];

    });
    NSLog(@"View did load called.");

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (id) init

{

self = [super init];

if (self) {

    [self setUrl: [NSURL URLWithString: @"http://website.com/Handler1.ashx"]];

    [self setInterface:@"http://website.com/Handler1.ashx"];

}

return self;

}

- (void)load:(NSData*)data completionHandler:(void(^)(NSURLResponse *response, NSData *data, NSError *error))handler

{

    NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"Loading... %@", s);



    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [request setHTTPMethod:@"POST"];

    [request setHTTPBody:data];

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [NSURLConnection sendAsynchronousRequest:request queue:queue

                           completionHandler:handler];

}


- (void) Getmembers:(NSString*)ausername completionHandler:(void(^)(NSDictionary *dictionary, NSError *error))handler

{

    NSMutableDictionary *d = [[NSMutableDictionary alloc] init];

    [d setValue:interface forKey:@"interface"];

    [d setValue:@"Getmembers" forKey:@"method"];

    NSMutableDictionary *p = [[NSMutableDictionary alloc] init];

    [p setValue:ausername forKey:@"username"];

    [d setValue:p forKey:@"parameters"];



    NSData *data = [NSJSONSerialization dataWithJSONObject:d options:0 error:nil];

    [self load:data completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

        NSLog(@"done");

        NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        NSLog(@"RAW response = %@", s);

        NSDictionary *d = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

        handler(d, error);

    }];

}

//login method
-(void) UserAuthentication:(NSString*)auserName passsword:(NSString*)apasssword completionHandler:(void(^)(NSDictionary *dictionary, NSError *error))handler

{
    //NSMutableArray *a = [[NSMutableArray alloc] init];
    NSMutableDictionary *d = [[NSMutableDictionary alloc] init];

    [d setValue:interface forKey:@"interface"];
    //[a setValue:interface forKey:@"interface"];

    [d setValue:@"UserAuthentication" forKey:@"method"];
    //[a setValue:@"UserAuthentication" forKey:@"method"];
    NSMutableDictionary *p = [[NSMutableDictionary alloc] init];

    [p setValue:auserName forKey:@"userName"];

    [p setValue:apasssword forKey:@"passsword"];

    [d setValue:p forKey:@"parameters"];
    //[a setValue:p forKey:@"parameters"];

    NSMutableArray *dictAllKeys=[NSMutableArray arrayWithArray:[d allKeys]];
    NSMutableArray *dictAllValues=[NSMutableArray arrayWithArray:[d allValues]];
    NSMutableArray *keysAndValues=[NSMutableArray arrayWithArray:[dictAllKeys arrayByAddingObjectsFromArray:dictAllValues]];

    //NSJSONWritingOptions *writingOptions;
    NSError *error;
    NSData *data = [NSJSONSerialization dataWithJSONObject:keysAndValues options:0 error:&error];

    [self load:data completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

        NSLog(@"done");

        NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        NSLog(@"RAW response = %@", s);

        //NSJSONReadingOptions *options;
        NSError *error2;
        NSDictionary *d = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error2];

        handler(d, error);

    }];

}

@end

最佳答案

Rest API SQL 设置不正确(集成安全性为真)。同样在 iOS 方面,我不应该传递一个数组来进行用户身份验证,而是传递一个字典。

关于ios - 为什么身份验证不适用于我的 ASP.NET REST API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28410394/

相关文章:

Javascript 在 Mac cocoa 应用程序中向 objective-c 发送消息

objective-c - 计算垂直于另一条线的中点的线

ios - 在 UITextView 更改时更改 UIReturnKeyType

javascript - 将 Stripe.com 和 Parse.com 与 iOS 集成 - 无法向客户添加订阅

asp.net - 使用 Python 对 ASP.NET 角色/成员身份创建的数据库中的原始用户名、散列、盐进行身份验证

c# - 如果没有及时收到网关服务器的确认,可能需要通过Socket重新发送数据

ios - UITapGestureRecognizer 与 UISearchBar

ios - 按钮未改变位置

iOS:在 cashes 文件夹和每个子文件夹中搜索视频文件

asp.net - 从 javascript 调用 ASP.Net 函数